Radio INPUT Element

 Radio INPUT Element

  • Radio buttons are used in groups.
  • It is described as a set of related options.
  • Only one radio button in a group can be selected at the same time.

 Syntax:-

<input type="radio">

  •  The radio group shares the same name (the value of the name attribute).
  • Once the radio group is created, selecting any radio button in the group and other radio buttons are automatically deselected.
  • The value attribute defines the unique value associated with each radio button.
  • It is not shown to the user, but this is sent to the server on "submit" to identify which radio button was selected.
  • The radio button is a very simple element.
  •  By default, if no radio button is specified as CHECKED, no default choice is made.

Example:-

<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>

 <?php

 $name_of_radio_button= $_POST ['name_of_radio_button'];

 ?>

Example:-

<form action="" method="post">

<input type="radio" name="radio" value="Radio 1">Radio 1

<input type="radio" name="radio" value="Radio 2">Radio 2

<input type="radio" name="radio" value="Radio 3">Radio 3

<input type="submit" name="submit" value="Get Selected Values" />

</form>

<?php

if (isset($_POST['submit'])) {

if(isset($_POST['radio']))

{

echo "You have selected :".$_POST['radio'];  //  Displaying Selected Value

}

}

?>

  •  For multiple radio buttons to be linked together to form a group and be processed as a single form element, they need to share the same name and different values (quite obviously). 

Post a Comment

0 Comments