$_POST spaces converted in underscores

I have an HTML dropdown menu that looks like this

<select name='not working random test!'>
<option value='0'>Select quantity:</option>
<option value='1'>1 room</option>
<option value='2'>2 rooms</option>
</select>

Is it even possible that, if I’m var_dumping $_POST, I see something like this?

["not_working_random_test!"]=>
string(1) "1"

This is causing some troubles with my engine: I expect the name I specify for the select to be the same. Why is this not happening?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

This is standard PHP behaviour. From the documentation:

Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].


The full list of field-name characters that PHP converts to _ (underscore) is the following (not just dot):

  • chr(32) (space)
  • chr(46) . (dot)
  • chr(91) [ (open square bracket)
  • chr(128) – chr(159) (various)

If both an open and a closing square bracket are present, they are not converted but the $_POST element becomes an array element.

<input name='hor[se'>
<input name='hor[se]'>

will become:

$_POST['hor_se'];
$_POST['hor']['se'];

::Reference

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply