我有一个循环,为用户提供了多个复选框:
<?php
while($personInfo = $selectPerson->fetch())
{
?>
<label>
<input type="checkbox" name="checkBoxValue[]" id="checkBoxValue" value="<?= $personInfo['title'] ?>"> <?= $personInfo['title'] ?>   
</label> |
<label>
<input type="checkbox" name="improper" id="improper" value="0"> Improper
</label>
<hr>
<?php
}
?>
问题是我设法通过以下方式恢复用户选择的每个复选框:
foreach($_POST['checkBoxValue'] as $selected)
{
echo $selected;
echo "<hr>";
}
但是我看不到如何知道是否为每个选中的复选框都选中了“不适当”复选框。
答案 0 :(得分:1)
<form method="post" action="#" name="stackOverflow">
<?php
$personInfo = [['id' => 2, 'title' => "Bernard"], [ 'id' => 3, 'title' => "Marc"]];
foreach ($personInfo as $info) {
?>
<label>
<input type="checkbox" name="checkBoxValue[<?php echo $info["id"] ?>]" id="checkBoxValue<?php echo $info["id"]; ?>" value="<?= $info['title'] ?>"
<?php if (isset($_POST['checkBoxValue'][$info["id"]])) { ?>
checked
<?php } ?>
>
<?= $info['title'] ?>   </label> | <label>
<input type="checkbox" name="improper[<?php echo $info["id"] ?>]" id="improper_<?php echo $info["id"]; ?> "
<?php if (isset($_POST['improper'][$info["id"]]) && "on" === $_POST['improper'][$info["id"]]) { ?>
checked
<?php } ?>
>
Improper
</label>
<hr>
<?php
}
?>
<input id="submit" type="submit" name="btn_validation" value="submit">