Validating A Drop-Down Menu With Array_Key_Exists()

<?phpphp$choices = array('eggs' => 'E',
                 'toast' => 'T',
                 'coffee' => 'C');
echo "<select name='food'>\n";
foreach ($choices as $key => $choice) {
   echo "<option value='$key'>$choice</option>\n";
}
echo "</select>";if (! array_key_exists($_POST['food'], $choices)) {
    echo "You must select a valid choice.";
}
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.