0

I've got multiple groups of checkboxes that PHP processes using the $_POST method, which creates an array for each checkbox group in the form. Now I need to produce array values of either 0 (not checked) or 1 (checked) for each checkbox, but the array currently only produces a key-value pair for checked checkboxes, ignoring the not-checked checkboxes.

Let's say I have 10 checkboxes in a group then I would also need 10 key-value pairs in my array, regardless of whether the checkbox is checked or not. Currently, if only 5 checkboxes are checked, my array produces 5 key-value pairs, shifting the values for the rest of my PHP script that is processing the values from 1 to 10.

How do I produce an array with as many key-value pairs as there are checkboxes, with values "0" for not-checked and "1" for checked checkboxes?

This is my example form:

(Notice the 5 checked and 5 not-checked inputs)

<form action="processingFile.php" method="post">
  <input type="checkbox" name="filter[]" value="1" checked="checked">
  <input type="checkbox" name="filter[]" value="1" checked="checked">
  <input type="checkbox" name="filter[]" value="1">
  <input type="checkbox" name="filter[]" value="1">
  <input type="checkbox" name="filter[]" value="1">
  <input type="checkbox" name="filter[]" value="1" checked="checked">
  <input type="checkbox" name="filter[]" value="1" checked="checked">
  <input type="checkbox" name="filter[]" value="1" checked="checked">
  <input type="checkbox" name="filter[]" value="1">
  <input type="checkbox" name="filter[]" value="1">
</form>

Currently produces this array:

$array = array(
  [0] => "1",
  [1] => "1",
  [2] => "1",
  [3] => "1",
  [4] => "1"
);

But this is the desired array:

$array = array(
  [0] => "1",
  [1] => "1",
  [2] => "0",
  [3] => "0",
  [4] => "0",
  [5] => "1",
  [6] => "1",
  [7] => "1",
  [8] => "0",
  [9] => "0"
);

My current script in processingFile.php as I'm trying to figure out how to get the full array of key-value pairs:

class Classname {

  public function methodName($array) {

    if (isset($array) && !empty($array)) {

      if (array_key_exists("0", $array)) {
        // Key 0 is present, do A...
      } else {
        // Key 0 is absent, don't do A...
      }

      if (array_key_exists("1", $array)) {
        // Key 1 is present, do B...
      } else {
        // Key 1 is absent, don't do B...
      }

      if (array_key_exists("2", $array)) {
        // Key 2 is present, do C...
      } else {
        // Key 2 is absent, don't do C...
      }

      // Different code for each key-value pair
      // Etc...

    } else {
      // Array is absent, do something else...
    }

  }

}

$newArray = new Classname;
$newArray->methodName($_POST);
  • Not sure there is a way to do that without using some JS. e.g. in jQuery, let elements = $("form [type=checkbox]"); array =[]; elements.each(function (index) { $(this).val($(this).prop("checked")); array.push($(this).prop("checked")); }); and then use those values in your post – SScotti Oct 25 '21 at 14:42
  • https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes – SScotti Oct 25 '21 at 14:52

1 Answers1

0

You can add an integer to the name of your checkboxes:

<form action="processingFile.php" method="post">
    <input type="checkbox" name="filter[0]" value="1" checked="checked">
    <input type="checkbox" name="filter[1]" value="1" checked="checked">
    <input type="checkbox" name="filter[2]" value="1">
    <input type="checkbox" name="filter[3]" value="1">
    <input type="checkbox" name="filter[4]" value="1">
    <input type="checkbox" name="filter[5]" value="1" checked="checked">
    <input type="checkbox" name="filter[6]" value="1" checked="checked">
    <input type="checkbox" name="filter[7]" value="1" checked="checked">
    <input type="checkbox" name="filter[8]" value="1">
    <input type="checkbox" name="filter[9]" value="1">
</form>

Submitting this and using print_r($_POST['filter']) will output:

Array
(
    [0] => 1
    [1] => 1
    [5] => 1
    [6] => 1
    [7] => 1
)
brombeer
  • 8,716
  • 5
  • 21
  • 27