Validating a drop-down menu with array_key_exists() : array_key_exists « Data Structure « PHP






Validating a drop-down menu with array_key_exists()

 
<?php

$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.";
}
?>
  
  








Related examples in the same category

1.Checking a
2.Checking for an element with a particular key
3.Testing for the Existence of a Key in an Array