Verifying Checkboxes are Checked with PHP
This is a tutorial for those who want to learn a bit more about PHP. It assumes some knowledge of PHP on your part or at least a certain comfort level.
The tutorial doesn't contain ready-made software, but how-to information.
Form names and values other than checkboxes and radio buttons are always sent to the PHP script the form is submitted to.
For checkboxes and radio buttons, the names and values are sent only if the checkbox or radio button is checked. The PHP script doesn't know about any that were left unchecked.
(JavaScript can determine whether or not certain ones are checked. That's because JavaScript is running on the same page as the form. However, some browsers don't run JavaScript, and neither do robots.)
With PHP, after the form is submitted, the page and the form are unavailable to the PHP script. Only the submitted form field names and values exist for the script. Unchecked checkboxes and radio buttons aren't submitted.
The PHP script the form is submitted to can be made to test form field values. Upon encountering an error, processing can be halted or an error message presented to the user.
This article presents a way to test whether or not certain checkboxes or radio buttons have been checked.
Because the field names of unchecked checkboxes and radio buttons don't exist for the PHP script, the script will spawn a "doesn't exist" warning if coded to test for values of unchecked checkboxes and radio buttons.
(Tip: For some spam prevention, the values of the checked checkboxes and radio buttons can be verified in the PHP script to contain the values as coded in the form. If different, the form submission almost certainly is spam.)
The Example Field
Let's use this checkbox field as an example:
<input type="checkbox" name="feeling" value="good">Feeling good?
If the checkbox is checked, the PHP script has the variable $_POST["feeling"] and its value is "good".
If the checkbox is unchecked, the PHP script doesn't know anything about it.
How To Test If Checkbox Was Checked Using The "PHP empty()" Function
To code a test for the existence of certain field names, the checkbox and radio field names must be known. Testing can be done with the PHP empty() function:
<?php if( empty($_POST["feeling"]) ) { echo "Checkbox was left unchecked."; } else { echo "Checkbox was checked."; } ?>
Here is a working example using the above code with the earlier presented example checkbox field. It's presented in an iframe to show the results without reloading this page.
The PHP empty() function works on both existent and non-existent variables. Thus, no warning or error message is spawned even if the variable doesn't exist.
If the variable is non-existent, or if the variable exists but is empty, then empty() returns true. Otherwise, if the variable exists and it contains data, empty() returns false.
Therefore, if the checkbox was left unchecked, the above PHP code's response is "Checkbox was left unchecked." If checked, its response is "Checkbox was checked."
Verifying
To establish whether or not a checkbox or radio button was checked, use the empty() function. Testing the value of a non-existent variable spawns a warning message.
If empty() returns false, the field was checked and the field's value can be accessed.
Will Bontrager