14

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael
  • 2,075
  • 7
  • 32
  • 46

3 Answers3

17

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

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Thanks for posting the second link! – Michael Apr 29 '12 at 07:27
  • Here is the corresponding HTML5 doc of the name attribute: https://www.w3.org/TR/html5/forms.html#naming-form-controls:-the-name-attribute – Nico O Mar 28 '17 at 15:18
  • @Jukka K. Korpela: What does it mean, if you write `Do not confuse the with the references to attributes declared as having NAME type.`? Where or what is the difference? – John Nov 25 '17 at 03:10
  • Attributes declared as having `NAME` type were an HTML 4 concept based on its being formally an SGML application. The `NAME` keyword in the formal definition of an attribute indicated that that the attribute value must start with a letter and may contain only letters, digits, and underscores. Attributes declared so have “NAME” in the “Type” column in the [Index of Attributes](https://www.w3.org/TR/html4/index/attributes.html) (in the HTML 4 spec). This concept is not used any more since HTML5, which does not use SGML formalisms. – Jukka K. Korpela Nov 26 '17 at 09:51
  • Something seems to be missing near *"...confuse the with the references"*. – Peter Mortensen Sep 30 '21 at 12:39
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
agmcleod
  • 13,321
  • 13
  • 57
  • 96
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
evan.stoddard
  • 698
  • 1
  • 5
  • 22