Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate checkboxes sent in array

Tags:

php

I have a form with multiple checkboxes, which I want to put into an array. I go by the example provided here: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html

So I've prepared the files:

checkboxes.php:

<form action="checkboxes2.php" method="post">
<input type="checkbox" name="tags[]" value="1" />1<br>
<input type="checkbox" name="tags[]" value="2" />2<br>
<input type="checkbox" name="tags[]" value="3" />3<br>
<input type="checkbox" name="tags[]" value="4" />4<br>
<input type="submit" value="Send" />
</form>

checkboxes2.php:

<?php
print_r($_POST["tags"]);
?>

Pretty simple...I realize I should only get the value of these textboxes and not if they have been selected or not. But I still get this error:

Undefined index: tags in checkboxes2.php on line 2

I have absolutely no idea what I did wrong here. I went by the example in the link above and did everything exactly the same (mainly copy/pasting and changing some parts like adding a submit button) but I don't get the output as shown in the example. I should at least get the values of each of these checkboxes, right?

What I want to do: I want to check the array of checkboxes, see which ones have been selected and add "yes" or "no" into a second array, like this:

<?php
  $number1 = $_POST["tags"];
  $number2 = array();

  foreach($number1 as $number1_output)
  {
    if(isset($number1_output))
    {
      $number2[] = "yes";
    }
    else
    {
      $number2[] = "no";
    }
  }

  print_r($number2);
?>

Well...it only half works. Only the checkboxes that have been selected are added to the array. So if I select "3" and "4" I get this:

Array ( [0] => yes [1] => yes )

What is the best way to deal with checkboxes in arrays and validating them?


2 Answers

Checkboxes which are not selected to do not send anything when the form submits. That means if NONE of those tags checkboxes are selected, there will be no tags attribute in the $_POST array.

As such, your code should be

<?
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if (isset($_POST['tags'])) {
      print_r($_POST['tags']);
   }
}
?>

The first line verifies that a POST has actually taken place, and the second line verifies that there's actually any data to dump out.

For the rest of your code, the isset is rather pointless... Since a checkbox is only present in the POST data if it was selected, there's no point in doing the isset() within your foreach loop - there'd be nothing to foreach on if there were no checkboxes, so by definition everything in the foreach will be isset() == true anyways.

As such, your foreach loop will only produce yes values, never a no.

A better workaround is to specify array keys within your form:

<input type="checkbox" name="tags[1]" value="1" />
<input type="checkbox" name="tags[2]" value="2" />
etc...

then have

<?php
if (b blah blah blah verify post stuff) {
    for ($i = 1; $i <= $max_tags_allowed; $i++) {
      if (isset($_POST['tags'][$i])) {
          $message2[] = 'yes';
      } else {
          $message2[] = 'no';
      }
    }
}
like image 199
Marc B Avatar answered Mar 02 '26 07:03

Marc B


Alternatively, if you want to have checkboxes that also send a value if they are not checked, you can add a Hidden input field before the checkbox:

<input type="hidden" name="tag[1]" value="off" />
<input type="checkbox" name="tag[1]" value="on />

Because the checkbox is only sent if it is set, but it overrides the hidden as it is later in the HTML, you will now always have tag[1] set with either 'off' or 'on' depending on whether or not the box was checked.

like image 29
Erik Avatar answered Mar 02 '26 06:03

Erik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!