-1

I have two page that each of them has one form. something like this:

// index.php

<form name="SampleForm" method="get"  action="search.php">
<input id="q" name="q" type="text"/>  //  text field
<input name="" type="submit" />       //  button
</form>


// search.php

<form name="SampleForm" method="get"  action="">
<input id="q" name="q" type="text"/>  //  text field
<input name="" type="submit" />       //  button
</form> 

Now, when I submit index.php the URL is something like this:

www.example.com/search.php?q=foo+bar

but when I submit search.php the URL is something like this:

www.example.com/search.php?q=foo%20bar

Why ? and how can I fix it ? (I want just +)


Edit: index.php to search.php is not ajax, but search.php to search.php is ajax. that's true, the reason of changing + to %20 is redirecting. so, is it possible to I convert from %20 to + in a redirected page ?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • @mario: He isn't asking whether the two forms are equivalent. For whatever reason, he wants to understand why his code is producing the two different (yet equivalent) forms. – Eric J. Jul 14 '15 at 00:11
  • @EricJ. Glad to understood my question exactly. and thanks ! – Shafizadeh Jul 14 '15 at 00:17
  • @EricJ. Wasn't that just a typo in the original edit? (Not sure of the relevancy. Question title and details still seem about the space serialization. The impicit `action=""` is just another browser automatism.) – mario Jul 14 '15 at 00:18
  • 1
    @mario: Only the OP can really say, but I interpret `how can I fix it ? (I want just +)` as meaning he knows they work the same, but he wants to understand specifically why he gets two different representations of space. – Eric J. Jul 14 '15 at 00:22
  • @EricJ. I admit I don't understand the obsession with that, or why that would constitute a 'problem' either way. (Nor does your answer cover the browser-specific variations). But reopened for whatever tutoring or scope change this is about... – mario Jul 14 '15 at 00:25
  • @mario: Yeah the only value seems to be "at least one inquiring mind wants to know." – Eric J. Jul 14 '15 at 00:36

3 Answers3

1

I don't see why the form in search.php would generate a URL including resutl.php, as that action was not specified.

The only way I see this happening is if search.php has code in it that builds a redirect URL, and that code uses the %20 encoding form.

Have a look at how search.php ends up redirecting to resutl.php. The answer lies there.

UPDATE

Based on the comments, you seem to be accessing this URL via Ajax. Look at where the URL is formed for the Ajax call.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • how look at how `search.php` ends up redirecting to `result.php` ? actually the structure of my website is something like *google*. `search.php` is homepage and `result.php` is page of searched. – Shafizadeh Jul 13 '15 at 23:27
  • You did not specify any action in the form code you show for search.php, so when someone submits that form, it will go to /search.php. The only way it will subsequently go to resutl.php is if there is some sort of redirect after the form is submitted. Look in the source of search.php at what happens when the form is submitted back to that page. – Eric J. Jul 13 '15 at 23:30
  • look, `index` to `search` is not ajax, but `search` to `search` is ajax, is it not related to the problem ? – Shafizadeh Jul 13 '15 at 23:45
  • 1
    @Sajad: (a) Why would you leave out a crucial detail like that from your question? (b) What "problem"? – Lightness Races in Orbit Jul 14 '15 at 00:00
  • @LightnessRacesinOrbit (a) what detail ? (b) I like to have clean URL, `%20` is disarranged. I want just `+`. – Shafizadeh Jul 14 '15 at 00:24
  • @Sajad: What does "disarranged" mean? I see no evidence of any problem here aside from maybe your personal taste. And the crucial detail is that one URL was generated by an AJAX call from a JavaScript library while the other was native to your browser. Not sure why you hid that from us. – Lightness Races in Orbit Jul 14 '15 at 00:26
  • @LightnessRacesinOrbit 'disarranged' means 'confused'. and for crucial detail, honestly I did not think to mentioning it be important. sorry – Shafizadeh Jul 14 '15 at 00:33
  • @Sajad: So in a question about two URLs having been generated differently, you didn't think that them being generated by two different mechanisms would be relevant? Still not getting why this is "confused": the `+` and the `%20` mean the same thing. _What is the actual problem??_ – Lightness Races in Orbit Jul 14 '15 at 00:33
  • Please drop this conversation from the comments to my answer. Feel free to take it to chat. – Eric J. Jul 14 '15 at 00:34
1

Well, by default a web form will use + for a GET form.

The difference is in index.php you are directly sending the data to result.php and hence the browser gives it as +

But in search.php, the result is given first to search.php (as action is "") and then I am guessing you are redirecting to result.php. This extra redirection is converting the + to %20

Finally, they both are equivalent - it doesnt really matter which one you use.

AbdealiLoKo
  • 3,261
  • 2
  • 20
  • 36
  • 1
    That's interesting. Which browser are you using ? I just tested your codes and I got + for both of them. – AbdealiLoKo Jul 13 '15 at 23:37
  • Either way, you may want to try using action="search.php" inside search.php also - maybe that will satisfy your browser. It seems to depend on the browser – AbdealiLoKo Jul 13 '15 at 23:38
1

Those two encoding forms are equivalent. The older RFC standard defined that the space should be encoded as a plus sign "+", the newer RFC standard defined to use percent encoding with the hexadecimal ASCII value.

All internet clients, search engines, spiders etc. know how to deal with it.

Sven
  • 69,403
  • 10
  • 107
  • 109