You want to make sure a valid radio button is selected from a group of radio buttons.
Use array_key_exists().
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas'); function display_form($array) { echo <<<HTML <form action = "$_SERVER[PHP_SELF]" method="post"> HTML; foreach ($array as $key =>$choice) { echo "<input type ='radio' name='fruit' value='$key' />$choice<br />"; } echo <<<EOF </select> <input type="submit" value="Submit" /> </form> EOF; } // validating the menu if (array_key_exists($_POST['fruit'], $fruits)) { echo 'Your selection is '.$_POST['fruit']; } else { echo "Select a valid choice."; display_form($fruits); } ?> </p> </body> </html> |