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!

Checkboxes and PHP

There are two things about form checkboxes that can trip up a PHP coder:

  1. Overwritten Values — When two or more checkboxes have the same form field name, their values may overwrite each other.

  2. Missing Checkboxes — The PHP software knows only about checkboxes that are checked before the form is submitted. Unchecked checkboxes are unseen and unknown.

Both of those gotchas are addressed with solutions in this article. We'll use these checkboxes for the illustrations.

I prefer watching I prefer reading I prefer napping

Overview of Both Gotchas

An overview of each gotcha follows this source code of the above illustration.

<input type="checkbox" name="preference" value="watch"> I prefer watching
<input type="checkbox" name="preference" value="read"> I prefer reading
<input type="checkbox" name="preference" value="nap"> I prefer napping

As you see, every checkbox has a name="preference" attribute.

It's not possible to think of every reason a person would want checkboxes with identical field names. It depends on project requirements or personal preference. But one reason is so checkboxes can be added and removed without having to update the PHP software to accomodate the form changes.

Overwritten values occur when more than one of the checkboxes is checked. In the PHP software that the form is submitted to, the variable $_POST['preference'] will contain the value of only one of the checked checkboxes.

(Which checkbox value is preserved depends on the form field order and how the PHP software receives the form information. Generally, it's the last checked checkbox value that clobbers the previous checkbox values.)

Missing checkboxes occur when none of the checkboxes are checked. In the PHP software that the form is submitted to, the variable $_POST['preference'] is missing. If the variable is used in the PHP software, the code will spawn an "undefined variable" warning.

The solutions for each of the gotchas are addressed separately.

Overwritten Values

There are two, mutually exclusive, solutions for overwritten values. The one you employ depends on your requirements.

  1. Unique form field names — When every field of a form has a different name attribute value, no name will overwrite another. The PHP code has the value available for each checkbox that was checked.

    If a checkbox with a name="watching" attribute is checked, then the PHP variable $_POST['watching'] contains the checkbox's value.

  2. Form field names as an array — Form field names can be made into an array by appending [] to the name. The name="preference[]" attribute is an example.

    The PHP code can then access the values of each checked checkbox with the $_POST['preference'][] array values.

Here is the illustration code with array name values.

<input type="checkbox" name="preference[]" value="watch"> I prefer watching
<input type="checkbox" name="preference[]" value="read"> I prefer reading
<input type="checkbox" name="preference[]" value="nap"> I prefer napping

And here is an overview of how the values can be processed in the PHP software.

foreach( $_POST['preference'][] as $value )
{
   // Do something with $value.
}

The foreach() loop in the above PHP code will process only the checkboxes that were checked. Remember that PHP software doesn't know about any unchecked checkboxes.

Missing Checkboxes

When a checkbox is unchecked, the browser never sends the information to the PHP software. It is omitted from the submission data.

Thus, PHP software doesn't know about any unchecked checkboxes.

"Unique form field names" handling:

If you have a checkbox with a name="watching" attribute and the checkbox is checked, then the checkbox value is available with the $_POST['watching'] PHP variable.

However, when the checkbox is unchecked, then there is no $_POST['watching'] PHP variable. Trying to access that variable results in an "undefined variable" warning.

A way around it is with code something like this:

if( isset($_POST['preference']) ) // Check to see if $_POST['preference'] is available.
{
    // Do something with $_POST['preference'].
}

The above code checks to see if the variable is available. Only if it is available is the variable processed.

"Form field names as an array" handling:

If you have checkboxes, all with name="preference[]" attributes, and at least one of the checkboxes is checked, then the checked checkbox values are available as $_POST['preference'][] array values.

However, when none of the name="preference[]" checkboxes are checked, then there is no $_POST['preference'] array. Trying to access the array results in an "undefined variable" warning.

A way around it is with code something like this:

if( isset($_POST['preference']) ) // Check to see if $_POST['preference'] is available.
{
   foreach( $_POST['preference'][] as $value )
   {
      // Do something with $value.
   }
}

The above code checks to see if the array is available. Only if it is available is the array processed.

An important note about the above code:

The isset() function uses the array name to do it's check. The foreach() function uses the array itself.

The array's name is $_POST['preference'] (no []) and the values within the array are accessed using $_POST['preference'][] (with the []).

Which to Use

Which method to use depends on your preferences.

If you are uncomfortable working with arrays, the "unique field value" method may be best. Another reason for using the "unique field value" method is if the PHP software is already written for handling unique field values and you're only updating the checkboxes.

If you prefer working with arrays (a positive is the ability to add and remove checkboxes in the future without modifying the PHP software), the "field names as an array" method may be the one to use.

Which you use, of course, is up to you and your requirements or preference. Both methods work to solve the missing values and overwriting values dilemmas.

(This article first appeared with an issue of the Possibilities newsletter.)

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.

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

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