0

I need to perform some very specific checks on a form, so I call a JS function on the submit and, if everything is ok, I call the "submit" method of that form using JS.

<form id="booking" action="booking.php" method="get">
    <input name="Name *" type="text">
    <input onclick="if (allOk()) document.forms['booking'].submit(); 
           return false;" type="submit" value="booknow">
</form>

This is the generated URL

booking.php?Name+*=marco

but...

print_r($_GET); 
... 
Array ( [Name_*] => marco )

I need to know how can I get the original name of the element, Name * in this particular case, from the Name_*

Why is there an underscore, where there should be a space?

I tried urldecode, rawurldecode, and some others but it is still not working. Is it because you cannot use spaces in the GET elements? Will it be safe to simply replace each underscore with a space?

I never had any problems encoding/decoding GET/POST values, but it looks like I have problems when the field name has to be encoded/decoded too.

PS: I know I should use POST, but it has the exact same problem, and I switched back to GET to debug it faster.

ZioBit
  • 905
  • 10
  • 29
  • dont use spaces, there is no need –  Oct 12 '16 at 05:10
  • It is not up to me. That's not a fixed string, it comes from a DB – ZioBit Oct 12 '16 at 05:12
  • of course its up to you, its your code lol –  Oct 12 '16 at 05:17
  • Of course I could not post the whole project, so I had to simplify it. The names of the fields come from a table of a db that contains several translations, so they change depending on the language, and also depending on which page the user is using the form. Sorry for asking but, if I asked "it's snowing but I need to go out", your answer would be "There is no need to go out", and if I told you that it's not up to me (because e.g. I need to go to work) your answer would be: "of course it's up to you, it's your life lol"? Well, that's not really helpful :( – ZioBit Oct 12 '16 at 05:23
  • just rename it if you can't fix the obviously misshaped db: `$_GET['Name *'] = $_GET['Name_*']; unset($_GET['Name_*']);` - also, why does it matter what names your GET-parameters are? aren't you sanitizing them afterwards, anyway? or do you pump the received data directly into your DB without even testing if it's valid? – Franz Gleichmann Oct 12 '16 at 05:26
  • I cannot post the reason why they have to be like that, but trust me, there is a valid reason (short, ok? The website owner wants to create his own forms with any name he likes, and specify which fields need to be mandatory, and using only a single text line, so I decided for a * at the end, and since I do not know how many/what they will be, I simply use the current available translation of that field name to store the data). But if I do $r["hello world"] = 5; print $r["hello world"]; it works, so spaces ARE allowed. Why they get messed up in my GET, I have no idea. – ZioBit Oct 12 '16 at 05:40
  • why is an input name language dependent? there is no reason it can't be the same name value for every language. its not a value the user ever sees –  Oct 12 '16 at 05:40
  • Ok, thank you again, but I am not discussing the fact that an input name should be language dependent or not. I am asking WHY if I put a space in a name in a form and I submit it, in PHP I get an underscore, regardless of the reason why there was a space in the first place. Then, we can discuss about how to better implement something, but first I'd like an answer about the first question :) – ZioBit Oct 12 '16 at 05:53
  • http://php.net/manual/en/language.variables.external.php your welcome –  Oct 12 '16 at 06:36
  • Thank you. Now I understood it. It's just a shame that: "PHP irreversibly modifies field names containing these characters in an attempt to maintain compatibility with the deprecated register_globals feature." To me, deprecated it should mean they should stop working sooner or later :( – ZioBit Oct 13 '16 at 03:14

0 Answers0