Validating a drop-down menu with in_array()
<?php
$choices = array('Eggs','Toast','Coffee');
echo "<select name='food'>\n";
foreach ($choices as $choice) {
echo "<option>$choice</option>\n";
}
echo "</select>";
if (! in_array($_POST['food'], $choices)) {
echo "You must select a valid choice.";
}
?>
Related examples in the same category