Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

php - $_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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...