Problem:- Validating Drop Down Menus Form Input
If you want to make sure that your user selects a valid choice from a drop-down menu.
Solution:-
<form method="POST"> <select name="food"> <option value="eggs">Eggs</option> <option value="Toast">Toast</option> <option value="Coffee">Coffee</option> <option value="Salad">Salad</option> </select> <input type="submit" name=""> </form> <?php $best_choice = array('eggs', 'Toast', 'Coffee'); if (!in_array($_POST['food'], $best_choice)) { echo "You must select a vaild choice"; }else{ echo "Your choice is {$_POST['food']}"; } ?>
Leave a Reply