PHP Form RadioButton
Description
The radio buttons are for single choice from multiple options. All radio buttons in the group have the same name attribute.
Only one button can be selected per group. As with checkboxes, use the value attribute to store the value that is sent to the server if the button is selected.
The value attribute is mandatory for checkboxes and radio buttons, and optional for other field types.
<label for="radioButtonField1">A radio button field</label>
<input type="radio" name="radioButtonField" id="radioButtonField1" value="radio1" />
<label for="radioButtonField2">Another radio button</label>
<input type="radio" name="radioButtonField" id="radioButtonField2" value="radio2" />
You can preselect a radio button using the same technique as for preselecting checkboxes.
Example
The following script is for index.htm. It has a group of radio buttons.
<html> //from w ww . jav a 2 s.c o m
<body>
<form action="index.php" method="post">
<b>Please select your favorite color wine:</b> <br>
<input type="radio" name="color" value="white"> White
<input type="radio" name="color" value="rose"> Rose
<input type="radio" name="color" value="red"> Red <br>
<input type="submit" value="Submit This Form">
</form>
</body>
</html>
The following script is for index.php. It reads the data from the form above.
<?php/*from ww w.j a v a 2 s.c om*/
$color = $_POST['color'];
if( ( $color != null ) )
{
$msg .= "a nice $color ";
echo( $msg );
}
?>