This article explains the methods you can use to handle multiple checkboxes in HTML forms using PHP. This means a group options on a HTML form that users can select many options they like. We would be using Sports Interests like: (Football, Baseball, Cricket, Softball, Handball, and Tennis).

The form page will look like this on a browser:

HTML Form for Multiple Checkboxes Example

All methods described in this article display the same form on a browser. This means that the same form is being manipulated by changing the name attribute of the HTML and changing the PHP handling process.

NOTE: Before we begin, we should remember that the value of the name attribute of an input form is converted to an array key in PHP POST/GET array.

HTML Checkbox is designed to send to the server only the checked input boxes. This means that only the checked interests would be received in the PHP POST/GET array. In the example bellow, only ‘Football’, ‘Baseball’, 'Handball' and 'Tennis' are checked, only Football, Baseball, Handball and Tennis are seen as a variable in the PHP POST/GET array.

<label for="your_name"><b>Your Name:</b> </label><input type="text" name="your_name" id="your_name" />


<p><b>Choose your sports interests:</b></p>
<input type="checkbox" name="fb" id="fb" value="Football" />  <label for="fb">Football</label>

<input type="checkbox" name="bb" id="bb" value="Baseball" />  <label for="bb">Baseball</label>

<input type="checkbox" name="cr" id="cr" value="Cricket" />   <label for="cr">Cricket</label><br />

<input type="checkbox" name="sb" id="sb" value="Softball" />   <label for="sb">Softball</label>

<input type="checkbox" name="hb" id="hb" value="" />           <label for="hb">Handball</label>

<input type="checkbox" name="tn" id="tn" />                    <label for="tn">Tennis</label>

When the above form is posted to the server, the PHP POST/GET array looks like this:

Array
(
    [your_name] => Obinna Nwafor
    [fb] => Football
    [bb] => Baseball
    [hb] => 
    [tn] => on
    [send] => Send
)

You notice Handball (hb) has its value attribute empty and Tennis (tn) has no value attribute specified.

 

Method #1

Different name attribute:

You can name each input checkbox by its distinct name:

<label for="your_name"><b>Your Name:</b> </label><input type="text" name="your_name" id="your_name" />
<p><b>Choose your sports interests:</b></p>
<input type="checkbox" name="football" id="fb" value="Football" />  <label for="fb">Football</label>
<input type="checkbox" name="baseball" id="bb" value="Baseball" />  <label for="bb">Baseball</label>
<input type="checkbox" name="cricket" id="cr" value="Cricket" />   <label for="cr">Cricket</label><br />
<input type="checkbox" name="softball" id="sb" value="Softball" />   <label for="sb">Softball</label>
<input type="checkbox" name="handball" id="hb" value="" />           <label for="hb">Handball</label>
<input type="checkbox" name="tennis" id="tn" />                    <label for="tn">Tennis</label>

This is how it is structured in the PHP POST/GET array

Array
(
    [your_name] => Obinna Nwafor
    [football] => Football
    [baseball] => Baseball
    [cricket] => Cricket
    [send] => Send
)

To handle it with PHP:

<?php
if(isset($_POST["send"])) //Checks if the send button is pressed
{
		echo $_POST["your_name"]; //print your name
	
        //check all the checkboxes if anyone is checked. this is a lot of work to do.
        //All checkboxes are checked one by one
	if(isset($_POST["football"]))
		echo $_POST["football"];
	
	if(isset($_POST["basketball"]))
		echo $_POST["basketball"];
		
	if(isset($_POST["cricket"]))
		echo $_POST["cricket"];
	
	if(isset($_POST["softball"]))
		echo $_POST["softball"];
	
	if(isset($_POST["handball"]))
		echo $_POST["handball"];
		
	if(isset($_POST["tennis"]))
		echo $_POST["tennis"];
}
?>

This will work fine as you can see above, but you have to test if the variable is set and get the value. Assuming there are 200 interests available for the user to choose from, and the user chose only one interest. You still have to test for 200 variables to retrieve only one value: this is not a good practice for me. Unless the form has only one group of checkboxes and no other type of input, then I know I am expecting only checked values. In this case, I will assume that all the elements in the PHP POST/GET array are the selected interests.

 

Method #2

Grouping the interests in an Indexed Arrays:

You can logically group the interests by manipulating the input name attribute of each checkbox. The empty squared brasses make it possible for the form to send unnamed elements to the server which allows PHP to form its own index for the newly created array (Indexed Array) and assign it to the variable named after the name before the empty squared brasses.

<label for="your_name"><b>Your Name:</b> </label><input type="text" name="your_name" id="your_name" />
<p><b>Choose your sports interests:</b></p>
<input type="checkbox" name="interests[]" id="fb" value="Football" /><label for="fb">Football</label>
<input type="checkbox" name="interests[]" id="bb" value="Baseball" /><label for="bb">Baseball</label>
<input type="checkbox" name="interests[]" id="cr" value="Cricket" /><label for="cr">Cricket</label>
<input type="checkbox" name="interests[]" id="sb" value="Softball" /><label for="sb">Softball</label>
<input type="checkbox" name="interests[]" id="hb" value="Handball" /><label for="hb">Handball</label>
<input type="checkbox" name="interests[]" id="tn" value="Tennis" /><label for="tn">Tennis</label>

This is how it is structured in the PHP POST/GET array

Array
(
    [your_name] => Obinna Nwafor
    [interests] => Array
        (
            [0] => Football
            [1] => Baseball
            [2] => Handball
            [3] => Tennis
        )

    [send] => Send
)

To handle it with PHP, iterate through the array and all elements are the values selected by the user:

<?php
if(isset($_POST["send"])) //Checks if the send button is pressed
{
	echo $_POST["your_name"]; //print your name
	
	if(isset($_POST["interests"])) //checks if any interest is checked
	{
		foreach($_POST["interests"] as $value) //Iterate the interest array and get the values
		{
			echo  $value;  //print the values
		}
	}
}
?>

What if you add indexes in the input forms ( interests[0], interests[1], interests[2] )? That means in the PHP GET/POST array, you will have numeric keys that are unsorted. Depending on the user’s selection:

Array
(
    [your_name] => Obinna Nwafor
    [interests] => Array
        (
            [1] => Baseball
            [3] => Softball
            [5] => Tennis
        )

    [send] => Send
)

Method #3

Grouping the interests in an Associative Arrays:

The element keys are specified between the squared brasses. This is almost the same with Method #2. The only difference is the type of array formed in the PHP POST/GET array.

<label for="your_name"><b>Your Name:</b> </label><input type="text" name="your_name" id="your_name" />
<p><b>Choose your sports interests:</b></p>
<input type="checkbox" name="interests[football]" id="fb" value="Football" /><label for="fb">Football</label>
<input type="checkbox" name="interests[baseball]" id="bb" value="Baseball" /><label for="bb">Baseball</label>
<input type="checkbox" name="interests[cricket]" id="cr" value="Cricket" /><label for="cr">Cricket</label>
<input type="checkbox" name="interests[softball]" id="sb" value="Softball" /><label for="sb">Softball</label>
<input type="checkbox" name="interests[handball]" id="hb" value="Handball" /><label for="hb">Handball</label>
<input type="checkbox" name="interests[tennis]" id="tn" value="Tennis" /><label for="tn">Tennis</label>

This is how it is structured in the PHP POST/GET array

Array
(
    [your_name] => Obinna Nwafor
    [interests] => Array
        (
            [football] => Football
            [cricket] => Cricket
            [softball] => Softball
            [tennis] => Tennis
        )

    [send] => Send
)

To handle it with PHP:

<?php
if(isset($_POST["send"])) //checks if the send button is pressed
{
	echo $_POST["your_name"]; //print your name
	
	if(isset($_POST["interests"])) //checks if any interest is checked
	{
		foreach($_POST["interests"] as $key=>$value) //Iterate the interest array and get the keys and values
		{
			echo $key . " => ". $value;  //print the keys and values
		}
	}
}
?>

 

What if no interest was checked by the user?

In this case, the ‘interests’ array variable (interests) would not be found in the PHP POST/GET array. That is why it is always a good practice to test if the variable exists before continuing with the iteration. It is also a form of validation where a user must select an item.

Multidimensional Checkbox Groups

At some point in your project, you could be faced with a complex form processing and you would like to isolate and group some related options to be selected by your user:

<label for="your_name"><b>Your Name:</b> </label><input type="text" name="your_name" id="your_name" />
<p><b>Choose your sports interests:</b></p>
<input type="checkbox" name="interests[sport][football]" id="fb" value="Football" /><label for="fb">Football</label>
<input type="checkbox" name="interests[sport][baseball]" id="bb" value="Baseball" /><label for="bb">Baseball</label>
<input type="checkbox" name="interests[sport][cricket]" id="cr" value="Cricket" /><label for="cr">Cricket</label>
<p><b>Choose your food interests:</b></p>
<input type="checkbox" name="interests[food][bread]" id="bd" value="Bread" /><label for="bd">Bread</label>
<input type="checkbox" name="interests[food][burger]" id="bg" value="Burger" /><label for="bg">Burger</label>
<input type="checkbox" name="interests[food][rice]" id="rc" value="Rice" /><label for="rc">Rice</label>

This is how it is structured in the PHP POST/GET array

Array
(
    [your_name] => Obinna Nwafor
    [interests] => Array
        (
            [sport] => Array
                (
                    [football] => Football
                    [baseball] => Baseball
                    [cricket] => Cricket
                )

            [food] => Array
                (
                    [burger] => Burger
                )

        )

    [send] => Send
)

To handle it with PHP:

<?php
if(isset($_POST["send"])) //checks if the send button is pressed
{
	echo $_POST["your_name"]; //echo name
	
	if(isset($_POST["interests"])) // checks if any interest is checked
	{
		if(isset($_POST["interests"]["sport"])) // checks if any sport interest is checked
		{
			foreach($_POST["interests"]["sport"] as $key=>$value) //Iterate the sport interest array and get the keys and values
			{
				echo $key . " => ". $value;  //print the keys and values
			}
		}
		
		if(isset($_POST["interests"]["food"])) // checks if any food interest is checked
		{
			foreach($_POST["interests"]["food"] as $key=>$value) //Iterate the food interest array and get the keys and values
			{
				echo $key . " => ". $value;  //print the keys and values
			}
		}
	}
}
?>

This is self explanatory when you have read the other methods.

In Conclusion, you can make anything out of the checkboxes as it would suite your development needs. Simply change the name attribute as you like and be flexible.

Spot my bugs and give me suggestions.

Hope this helps.