Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

Support Area – Ask Your Question Here

The "Code in articles help" forum at the Willmaster.com support area is the place to get information about implementing JavaScript and other software code found on Willmaster.com

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC