PHP Tutorial - PHP Form RadioButton






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> 
 <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
  $color = $_POST['color'];
  if( ( $color != null ) )
  {
    $msg .= "a nice $color ";
    echo( $msg );
  }
?>