Exchange %20 for + in GET search

Asked

Viewed 226 times

3

When I do a search via GET through a form the url looks like this:

test.com.br? search.php? q=Phrase%20what%20am%20searching

I’d like to keep it that way:

test.com.br? search.php? q=Phrase+what+am I+seeking

Anybody know how to do that? Reference: https://www.globo.com/search/? q=testing+search

Form code:

<form class="form-inline" action="busca.php" method="get" >        
        <div class="form-group" style="width:100%">
            <label for="numero" class="control-label">Busca</label><br>
            <input class="form-control" id="termo" name="termo" type="text" value="" style="width:100%">
        </div>
<div class="form-group" style="width:100%">
            <br>
            <button type="submit" class="btn btn-primary" style="width:100%!important; padding:6px 12px;"><i class="fa fa-search" aria-hidden="true"></i> Pesquisar</button>
        </div><!-- /form-group -->
</form>
  • Enter the form code. Note that %20 is correct for URL, and + for form.

  • It may simply be a enctype"application/x-www-form-urlencoded" missing

  • I put the code

1 answer

7


Both the %20 as to the + are valid characters to represent the space, being the first used in Urls and the second in Forms, by default (for example, in querystring).

The + was specified in this RFC:

http://www.faqs.org/rfcs/rfc3986.html

And then it was replaced by %20.

In PHP itself, the urlencode() uses %20, and for compatibility purposes, the rawurlencode() with the behavior of RFC 3986.

What defines the upload formatting on the browser side is the enctype, that is not in your form original:

<form enctype="application/x-www-form-urlencoded">

Handbook

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Form

May happen of newer applications use the %20 "in the box" (remembering that in a normal form this is done by browser and not on the PHP side). It is important to use + for the sake of leaving the link with a different aesthetic, independent of the browser, will have to make a POST for a PHP that gives a redirect using rawurlencode, but I wouldn’t recommend plastering the application that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.