You want to make sure a valid radio button is selected from a group of radio buttons.
Use an array of values to generate the menu. Then validate the input by checking that the submitted value is in the array. Example 9-13 uses array_key_exists( ) to do the validation.
Validating a radio button
The radio button validation in Example 9-13 is very similar to the drop-down menu validation in Example 9-12. They both follow the same pattern'define the data that describes the choices, generate the appropriate HTML, and then use the defined data to ensure that a valid value was submitted. The difference is in what HTML is generated.
Another difference between drop-down menus and radio buttons is how defaults are handled. When the HTML doesn't explicitly specify a default choice for a drop-down menu, the first choice in the menu is used. However, when the HTML doesn't explicitly specify a default choice for a set of radio buttons, no choice is used as a default.
To ensure that one of a set of radio buttons is chosen in a well-behaved web browser, give the default choice a checked="checked" attribute. In addition, to guard against missing values in hand-crafted malicious requests, use isset( ) to ensure that something was submitted for the radio button, as described in Recipe 9.2.
9.6.4. See Also
Recipe 9.2 for information on validating required fields; documentation on array_key_exists( ) at http://www.php.net/array_key_exists.