InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Validating Form Input: Drop-Down Menus in PHP
PHP

Validating Form Input: Drop-Down Menus in PHP

InfinityCoder November 28, 2016

You want to make sure that a valid choice was selected from a drop-down menu generated by the HTML <select/> element.

Use an array of values to generate the menu. Then validate the input by checking that the value is in the array. Example 9-10 uses in_array() to do the validation.
Example 9-10. Validating a drop-down menu with in_array()

1
2
3
4
5
6
7
8
9
10
11
12
// Generating the menu
$choices = array('Eggs','Toast','Coffee');
echo "<select name='food'>\n";
foreach ($choices as $choice) {
   echo "<option>$choice</option>\n";
}
echo "</select>";
 
// Then, later, validating the menu
if (! in_array($_POST['food'], $choices)) {
   echo "You must select a valid choice.";
}

The menu that Example 9-10 generates is:

1
2
3
4
5
6
7
8
9
10
11
<select name='food'>
<option>Eggs</option>
<option>Toast</option>
<option>Coffee</option>
</select>
 
<select name='food'>
<option>Eggs</option>
<option>Toast</option>
<option>Coffee</option>
</select>

To work with a menu that sets value attributes on each <option/> element, use array_key_exists() to validate the input, as shown in Example 9-11.
Example 9-11. Validating a drop-down menu with array_key_exists()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Generating the menu
$choices = array('eggs' => 'Eggs Benedict',
                 'toast' => 'Buttered Toast with Jam',
                 'coffee' => 'Piping Hot Coffee');
echo "<select name='food'>\n";
foreach ($choices as $key => $choice) {
   echo "<option value='$key'>$choice</option>\n";
}
echo "</select>";
 
// Then, later, validating the menu
if (! array_key_exists($_POST['food'], $choices)) {
   echo "You must select a valid choice.";
}

The menu that Example 9-11 generates is:

1
2
3
4
5
<select name='food'>
<option value='eggs'>Eggs Benedict</option>
<option value='toast'>Buttered Toast with Jam</option>
<option value='coffee'>Piping Hot Coffee</option>
</select>

The methods in Examples 9-10 and 9-11 differ in the kinds of menus that they generate.
Example 9-10 has a $choices array with automatic numeric keys and outputs <option/> elements.

Example 9-11 has a $choices array with explicit keys and outputs <option/> elements with value attributes drawn from those keys.
In either case, the validation strategy is the same: make sure that the value submitted for the form element is one of the allowed choices.

For requests submitted by wellbehaved browsers, this validation rule never fails—web browsers generally don’t let you make up your choice for a drop-down menu.

Remember, though, that there’s nothing requiring that requests to your PHP program come from a well-behaved web browser.
They could come from a buggy browser or from a bored 11-year-old with a copy of the HTTP specification in one hand and a command-line telnet client in the other.

Because you always need to be mindful of malicious, hand-crafted HTTP requests, it’s important to validate input even in circumstances where most users will never encounter an error.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Appending One Array to Another in PHP
You want to combine two arrays into one. Use array_merge(): …

Appending One Array to Another in PHP

Finding PEAR Packages
You want a listing of PEAR packages. From this list …

Finding PEAR Packages

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap