Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_POST spaces converted in underscores

Tags:

post

php

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?

like image 429
Saturnix Avatar asked Jun 13 '13 16:06

Saturnix


1 Answers

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

like image 73
Tim Cooper Avatar answered Nov 10 '22 17:11

Tim Cooper