dsdt66064367 2018-06-19 12:54
浏览 113

在循环PHP中选择了多个复选框

I have a loop that offers several checkboxes to the user:

<?php
while($personInfo = $selectPerson->fetch())
{
?>

    <label>
    <input type="checkbox" name="checkBoxValue[]" id="checkBoxValue" value="<?= $personInfo['title'] ?>"> <?= $personInfo['title'] ?>&nbsp&nbsp&nbsp
    </label> | 
    <label>
    <input type="checkbox" name="improper" id="improper" value="0"> Improper
    </label>
    <hr>

<?php
}
?>

The thing is that I manage to recover each checkbox selected by the user with:

foreach($_POST['checkBoxValue'] as $selected)
{
    echo $selected;
    echo "<hr>";
}

But I do not see how I can know if for each checkbox selected, the checkbox "Improper" is also selected.

  • 写回答

1条回答 默认 最新

  • doujia1679 2018-06-19 13:44
    关注
    <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'] ?>&nbsp&nbsp&nbsp</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">
    

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错