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: Checkboxes in PHP
PHP

Validating Form Input: Checkboxes in PHP

InfinityCoder November 28, 2016

You want to make sure only valid checkboxes are checked.

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-13 figures out whether a checkbox was checked, unchecked, or had an invalid value submitted.
Example 9-13. Validating a single checkbox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Generating the checkbox
$value = 'yes';
echo "<input type='checkbox' name='subscribe' value='yes'/> Subscribe?";
 
// Then, later, validating the checkbox
if (filter_has_var(INPUT_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-14.
Example 9-14. Validating a group of checkboxes

1
2
3
4
5
6
7
8
9
10
11
12
// 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.";
}

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-14 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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Storing Arbitrary Data in Shared Memory in PHP
You want a chunk of data to be available to …

Storing Arbitrary Data in Shared Memory in PHP

Deleting Cookies in PHP
You want to delete a cookie so a browser doesn’t …

Deleting Cookies in PHP

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

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

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 © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap