Using Radio Buttons

This isn't too tricky but can be quite useful in real web programs. Radio buttons normally come in groups and the user expected to choose only one of them.

<html>
<head>
<title>Radio</title>
</head>
<body>
<h1>Processing Radio Buttons</h1>
<?php
if (isset($_POST['subject']))
{
   $theychose = $_POST['subject'];
   echo "<p>You chose ".$theychose.". Why?</p>";
}
else
{
   echo "<form action='radio.php' method='POST'>
   <p>Select only one of the following items.</p>
   <p><input type='radio' name='subject' value='Geography'>Geography<br>
   <input type='radio' name='subject' value='Computing' checked='CHECKED'>Computing<br>
   <input type='radio' name='subject' value='Mathematics'>Mathematics<br>
   <input type='radio' name='subject' value='Physics'>Physics</p>
   <input type='submit' value='Submit The Form'>
   </form>";
}
?>
</body>
</html>

Notice that all of the radio buttons have the same name. When the form is submitted, the value of the checked radio button is retrievable using that variable.

Clearly, radio buttons are a useful user interface element that can be used as an alternative to dropdown boxes. They are mainly used when it makes sense to display all of the selectable items on the page.