Michael
Michael

Reputation: 2187

Valid value for the "name" attribute in 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.

Upvotes: 14

Views: 6090

Answers (3)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201886

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'].

Upvotes: 17

evan.stoddard
evan.stoddard

Reputation: 698

You should name your input items with text, not numbers. They should not contain any characters such as ., ,, !, and ?. This can cause problems. For more information submitting the data, go to PHP and HTML Radio Buttons

Upvotes: 0

agmcleod
agmcleod

Reputation: 13621

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.

Upvotes: 1

Related Questions