How to use checkboxes with PHP

This tutorial will show you how to use HTML checkboxes with PHP.

Before you attempt to process checkbox values, there are a few important things that you need to know:

  • The name that you assign to your checkbox won’t exist as a POST variable if the checkbox in question hasn’t been ticked by the user. If you are not careful with this, it may result in an undefined index error. To prevent this, you will need to check if the POST variable exists before you attempt to check its value.
  • Checkboxes should only be used for multiple-choice options. In cases where a single choice must be made, a radio button or a select option will work better.

Single checkbox example

In this “Terms and Conditions” example, we will process a single checkbox with PHP:

//Set "terms accepted" to false by default.
$termsAccepted = false;

//If the POST variable "terms_of_service" exists.
if(isset($_POST['terms_of_service'])){
    //Checkbox has been ticked.
    $termsAccepted = true;
}

//Print it out for example purposes.
if($termsAccepted){
    echo 'Terms of Service checkbox ticked!';
}

?>

<form action="" method="post">
    <label>
        I accept the terms of service.
        <input type="checkbox" name="terms_of_service" value="Y">
    </label>
    <input type="submit" value="Register">
</form>

As you can see, we had to check to see if the POST variable “terms_of_service” existed. We did this by using the isset function. If the name of the checkbox exists as a POST variable, then it is safe to presume that the user has ticked it.

But what about multiple-choice checkboxes? Well, that’s a little more complex.

Multiple checkboxes & PHP

In this example, we allow our users to select their favorite fruits:

//Valid options. A whitelist of allowed options.
$fruitOptions = array(
    'Orange',
    'Apples',
    'Strawberries',
    'Grapes'
);

//Empty array by default.
$fruits = array();

//If the POST var "fruits" is a valid array.
if(!empty($_POST['fruits']) && is_array($_POST['fruits'])){
    //Loop through the array of checkbox values.
    foreach($_POST['fruits'] as $fruit){
        //Make sure that this option is a valid one.
        if(in_array($fruit, $fruitOptions)){
            //Add the selected options to our $fruits array.
            $fruits[] = $fruit;
        }
    }
}

var_dump($fruits);

?>

<form action="" method="post">
    <label>
        Oranges
        <input type="checkbox" name="fruits[]" value="Orange">
    </label>
    <label>
        Apples
        <input type="checkbox" name="fruits[]" value="Apples">
    </label>
    <label>
        Strawberries
        <input type="checkbox" name="fruits[]" value="Strawberries">
    </label>
    <label>
        Grapes
        <input type="checkbox" name="fruits[]" value="Grapes">
    </label>
    <input type="submit" value="Register">
</form>

Step-by-step explanation:

  1. We set up a whitelist array, which contains all of the checkbox values that the user is allowed to select. This is important as we don’t want to find ourselves in a situation where a user is modifying our form values with Firebug or Chrome’s Developer Tools (this would allow them to add checkbox options that don’t exist).
  2. We created an empty PHP array called $fruits. Once the form has been processed, this array will contain all of the values that the user has ticked.
  3. If you are using multiple-choice checkboxes, then it is important to note that your POST variable will be an array.
  4. In the example above, we loop through the POST array containing the selected checkbox values. We also validate them by making sure that they are in our whitelist array ($fruitOptions).
  5. If you look at the form, you’ll see that we named each checkbox “fruits[]”. The square brackets tell PHP that these checkbox values should be held in an array.