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 />";
}
}
}
CodePudding user response:
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']))
....
}
CodePudding user response:
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.