Recipe 9.7. Validating Form Input: Checkboxes


9.7.1. Problem

You want to make sure only valid checkboxes are checked.

9.7.2. Solution

For a single checkbox, ensure that if a value is supplied, it's the correct one. If a value isn't supplied for the checkbox, then the box wasn't checked. Example 9-14 figures out whether a checkbox was checked, unchecked, or had an invalid value submitted.

Validating a single checkbox

<?php // Generating the checkbox $value = 'yes'; echo "<input type='checkbox' name='subscribe' value='yes'/> Subscribe?"; // Then, later, validating the checkbox if (isset($_POST['subscribe'])) {     // A value was submitted and it's the right one     if ($_POST['subscribe'] == $value) {         $subscribed = true;     } else {         // A value was submitted and it's the wrong one         $subscribed = false;         print 'Invalid checkbox value submitted.';     } } else {     // No value was submitted     $subscribed = false; } if ($subscribed) {     print 'You are subscribed.'; } else {     print 'You are not subscribed'; } 

For a group of checkboxes, use an array of values to generate the checkboxes. Then, use array_intersect( ) to ensure that the set of submitted values is contained within the set of acceptable values, as shown in Example 9-15.

Validating a group of checkboxes

<?php // Generating the checkboxes $choices = array('eggs' => 'Eggs Benedict',                  'toast' => 'Buttered Toast with Jam',                  'coffee' => 'Piping Hot Coffee'); foreach ($choices as $key => $choice) {    echo "<input type='checkbox' name='food[]' value='$key'/> $choice \n"; } ?> // Then, later, validating the radio button submission if (array_intersect($_POST['food'], array_keys($choices)) != $_POST['food']) {     echo "You must select only valid choices."; } ?> 

9.7.3. Discussion

For PHP to handle multiple checkbox values properly, the checkboxes' name attribute must end with [], as described in Recipe 9.17. Those multiple values are formatted in $_POST as an array. Since the checkbox name in Example 9-15 is food[], $_POST['food'] holds the array of values from the checked boxes.

The array_intersect( ) function finds all of the elements in $_POST['food'] that are also in array_keys($choices). That is, it filters the submitted choices ($_POST['food']), only allowing through values that are acceptable'keys in the $choices array. If all of the values in $_POST['food'] are acceptable, then the result of array_intersect($_POST['food'], array_keys($choices)) is an unmodified copy of $_POST['food']. So if the result isn't equal to $_POST['food'], something invalid was submitted.

Checkboxes have the same issues with default values as do radio buttons. So just as with radio buttons, use the rules in Recipe 9.2 to determine that something was submitted for the checkbox before proceeding with further validation.

9.7.4. See Also

Recipe 9.2 for information about validating required fields; documentation on array_intersect( ) at http://www.php.net/array_intersect.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net