Get all values from multiple checkboxes in PHP

In this article, I am going to share a PHP trick with you which going to be very useful. Developers often have to deal with multiple checkboxes when they work in a project in PHP.

Even, as a developer, I also have to get all the checked values from a number of checkboxes with the same name. Often it is needed in projects.

Are you thinking about how to do that?

Well, there is a little bit tricky. Well, you can’t get all the values from checked checkboxes if you give the name of the inputs as usual. For example, see the form below with checkboxes input below with the name “color”:

<input type="checkbox" name="color" value="Blue"> Blue

We can easily get the values by its name in PHP using the $_POST. But, the problem comes when we try to get all the values from our multiple inputs with same name.

So here we are going to do a trick. We will give the input name “color[]” instead of “color”:

<input type="checkbox" name="color[]" value="Blue"> Blue

Now you may think about why we did it. The input name “color[]” looks like something unusual when we get values from a single checkbox type input.

Here we have actually initialized an array by giving the checkbox type input name “color[]”. If there are multiple checkboxes with this name in our form, all these values will be stored in the array. All the values will be put one by one when we select multiple checkboxes with the name “color[]”.

PHP code example to get all values from selected checkboxes

Now, look at our HTML form below:

<form action="action_page.php" method="POST">
  <input type="checkbox" name="color[]" value="Red"> Red<br>
  <input type="checkbox" name="color[]" value="Blue"> Blue<br>
  <input type="checkbox" name="color[]" value="Black"> Black<br>
  <input type="checkbox" name="color[]" value="Green"> Green<br>
  <input type="checkbox" name="color[]" value="Yellow"> Yellow<br>
  <input type="submit" name="submit" value="submit">
</form>

Now in our “action_page.php” page, we have PHP code that is given below:

if (isset($_POST['color'])) {

  foreach ($_POST['color'] as $color) {
    echo $color."<br>";
  }
}

Now if we select some of our checkboxes and submit our form, then we will able to see those selected input values line by line just like you can see below:

Blue
Green
Yellow

In our above code, we have used foreach loop to extract and print each value that we have checked from our checkbox.

Also, read:

 

Here, actually our values store in the $_POST[“color”]. If we print it, then you will see an array thatis given below:

Array
(
    [0] => Blue
    [1] => Green
    [2] => Yellow
)

Now it is an easy task for us to get values from this array if we are familiar with the PHP foreach loop. We use the foreach loop to get each element values from our array.

I hope, you have got the idea from our example and you will be able to get values from multiple checked checkboxes.

Leave a Reply

Your email address will not be published. Required fields are marked *