0

I am building an application with php

I have a form which contains an array of checkboxes as shown in the picture below

I want to the values accordingly but it is not working as expected

The form was generated with php for loop

HTML

<form action="submitchecklist.php" method="post">
  <table border="2">
  <thead>
      <th colspan="2">PLUMBING</th>  
  </thead>
<tbody>
    <tr>
        <td></td>
        <td>
            Leakage
        </td>
        <td>Heater</td>
    </tr>

    <?php
    for ($i = 201; $i <= 215; $i++) {
        echo '
            <tr>
            <td>' . $i . '</td>
            <td>
                <input type="checkbox" value="yes" name="leak_yes[]" id="">Yes
                <input type="checkbox" value="no" name="leak_no[]" id="">No
            </td>
            <td>
                <input type="checkbox" name="heat_yes[]" id="">Yes
                <input type="checkbox" name="heat_no[]" id="">No
            </td>
            </tr>
            '
    } ?>

</tbody>
</table>
</form>

I am trying to get the selected values but not working as expected.

leaked_no shows only when leaked yes is selected otherwise it will ignored

If I select 6 leaked_yes and 8 leaked_no it shows 7 leaked_yes and 7 leaked_no.

submitchecklist.php

if (isset($_POST['leak_yes'])) {
    $leak_no = $_POST['leak_no'];
    $leak_yes = $_POST['leak_yes'];
    if (is_array($_POST['leak_yes'])) {
        $initalValue = 201;
        foreach ($_POST['leak_yes'] as $key => $value) {
            echo ($initalValue + $key);
            echo (isset($leak_yes[$key])) ? $leak_yes[$key] : "";
            echo "<br />";
            echo (isset($leak_no[$key])) ? $leak_no[$key] : "";
            echo "<br />";
        }
    }
}

enter image description here

enter image description here

etranz
  • 891
  • 2
  • 10
  • 29
  • Does this answer your question? [POST unchecked HTML checkboxes](https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes) – Ken Lee Jan 12 '23 at 04:34
  • 1
    Shouldn't you be using radio buttons instead of checkboxes? – S. Imp Jan 12 '23 at 04:35
  • @KenLee it did not answer my question. – etranz Jan 12 '23 at 04:38
  • @S.Imp I wont be able to use array with radio but do you have a suggestion to my question – etranz Jan 12 '23 at 04:40
  • @etranz to be honest, i don't really know what your question is. Since you are using checkboxes, someone could check both YES and NO. Your HTML has no
    tag and no submit button. You appear to show some output or something, but I really don't know what your actual code looks like or what you are asking.
    – S. Imp Jan 12 '23 at 04:48
  • @S.Imp I have updated my question. I have multple array of checkboxes. I want to display selected ones when I submit the form – etranz Jan 12 '23 at 05:12
  • @etranz What does `print_r($_POST) ` give you? Also, remove those ugly empty `id` attributes. – nice_dev Jan 12 '23 at 05:18
  • [php checkboxes always showing checked](https://stackoverflow.com/a/1698887/2943403) – mickmackusa Jan 12 '23 at 05:36

2 Answers2

1

Don't use the name like leak_yes[], instead define the name as the following form:

leak[$i][yes].

The corresponding array element will be missing when the Yes or No checkbox is unchecked, but you are able to use foreach and isset to skip them.

HTML example:

<input type="checkbox" value="yes" name="leak['.$i.'][yes]" id="">Yes
<input type="checkbox" value="no" name="leak['.$i.'][no]" id="">No

PHP example:

foreach($_POST['leak'] as $i => $props)
{
    if(isset($props['yes']))
       ....
    if(isset($props['no']))
       ....
}
shingo
  • 18,436
  • 5
  • 23
  • 42
0

You can always inspect $_POST data by using a command like var_dump. If you put this in submitchecklist.php, the file that is handling this form POST:

var_dump($_POST);

Then you can see that the data that arrives via POST probably isn't doing what you want. The way you have your form constructed just creates unhelpful, consecutively indexed arrays that contain strings of 'yes' or 'no' for any checkboxes. All this tells you is how many yeses and how many nows where checked. You don't know, for example, which checkboxes were checked for row i:

array(3) {
  ["leak_yes"]=>
  array(7) {
    [0]=>
    string(3) "yes"
    [1]=>
    string(3) "yes"
    [2]=>
    string(3) "yes"
    [3]=>
    string(3) "yes"
    [4]=>
    string(3) "yes"
    [5]=>
    string(3) "yes"
    [6]=>
    string(3) "yes"
  }
  ["leak_no"]=>
  array(8) {
    [0]=>
    string(2) "no"
    [1]=>
    string(2) "no"
    [2]=>
    string(2) "no"
    [3]=>
    string(2) "no"
    [4]=>
    string(2) "no"
    [5]=>
    string(2) "no"
    [6]=>
    string(2) "no"
    [7]=>
    string(2) "no"
  }
  ["submit"]=>
  string(6) "submit"
}

Try inserting $i in the square brackets for each checkbox name :

    for ($i = 201; $i <= 215; $i++) {
        echo '
            <tr>
            <td>' . $i . '</td>
            <td>
                <input type="checkbox" value="yes" name="leak_yes[' . $i . ']">Yes
                <input type="checkbox" value="no" name="leak_no[' . $i . ']">No
            </td>
            <td>
                <input type="checkbox" name="heat_yes[' . $i . ']">Yes
                <input type="checkbox" name="heat_no[' . $i . ']">No
            </td>
            </tr>
            ';
    }

and you should be able to connect a given checkbox value to a particular row:

array(3) {
  ["leak_yes"]=>
  array(7) {
    [201]=>
    string(3) "yes"
    [203]=>
    string(3) "yes"
    [204]=>
    string(3) "yes"
    [208]=>
    string(3) "yes"
    [213]=>
    string(3) "yes"
    [214]=>
    string(3) "yes"
    [215]=>
    string(3) "yes"
  }
  ["leak_no"]=>
  array(8) {
    [202]=>
    string(2) "no"
    [205]=>
    string(2) "no"
    [206]=>
    string(2) "no"
    [207]=>
    string(2) "no"
    [209]=>
    string(2) "no"
    [210]=>
    string(2) "no"
    [211]=>
    string(2) "no"
    [212]=>
    string(2) "no"
  }
  ["submit"]=>
  string(6) "submit"
}

Note how the array has $i => 'yes' or whatever.

S. Imp
  • 2,833
  • 11
  • 24