Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid value for the "name" attribute in HTML

Tags:

html

I use PHP to get radio button values from an HTML page. My HTML looks like this:

<input type="radio" name="1.1" value="yes">
<input type="radio" name="1.1" value="no">

<input type="radio" name="1" value="yes">
<input type="radio" name="1" value="no">

The result is that $_POST['1'] returns a value, but $_POST['1.1'] returns nothing. I checked the HTML 4 specifications, say value for the name attribute only starts with a letter, but 1 is not a letter. How come it gets returned while 1.1 does not? Or is there some other magic happening here? I use the latest version of Chrome.

like image 504
Michael Avatar asked Apr 29 '12 02:04

Michael


2 Answers

By HTML rules, the name attribute may have any value: it is declared with CDATA type. Do not confuse this attribute with the references to attributes declared as having NAME type. See 17.4 The INPUT element, name = cdata [CI].

In the use of $POST[...] in PHP, you need to note this PHP rule: “Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].” See Variables From External Sources.

So $_POST['1'] should work as is and does work, but instead of $_POST['1.1'] you need to write $_POST['1_1'].

like image 102
Jukka K. Korpela Avatar answered Oct 10 '22 01:10

Jukka K. Korpela


Try substituting the period for something else like a hyphen. In both the form and the PHP code. Periods are generally used for a . in the extension name.

When it comes to key names for parameters in either GET or POST headers, you want to only use alphanumeric characters, with some special characters generally. Such as hyphens, underscores, etc. You can always do a URL encode if you need to as well.

like image 26
agmcleod Avatar answered Oct 10 '22 01:10

agmcleod