PHP Form RadioButton

In this chapter you will learn:

  1. What is PHP Form RadioButton
  2. Example - 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   jav a2 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//  j a va2  s.c  o  m
  $color = $_POST['color'];
  if( ( $color != null ) )
  {
    $msg .= "a nice $color ";
    echo( $msg );
  }
?>

Next chapter...

What you will learn in the next chapter:

  1. What is the form submit button
Home » PHP Tutorial » PHP Form
PHP HTML Forms
PHP GET and POST
PHP Form Elements
PHP Form TextField
PHP Form Password Field
PHP Form Textarea
PHP Form CheckBox
PHP Form Select
PHP Form RadioButton
PHP Form Submit
PHP Form Reset
PHP Form Push Button
PHP Form Image
PHP Form Hidden Field
PHP Split Form Across Pages
PHP Form Validation
PHP Form File Upload
PHP Form Demo
PHP Form Empty Fields
PHP Form Multi-Value Fields
PHP Redirect after a Form Submission