【问题标题】:loop through checkboxes - how to get all checked checkboxes?循环通过复选框 - 如何获取所有选中的复选框?
【发布时间】:2014-02-03 19:50:57
【问题描述】:

我有以下一组复选框:

<div class="chb_group">
   <span class="custom_chb_wrapper">
       <input class="zcheckbox" type="checkbox" value="164" name="categoriesfilters">
       <label>Air Condition A/C</label>
   </span>
</div>
<div class="chb_group">
    <span class="custom_chb_wrapper">
        <input class="zcheckbox" type="checkbox" value="165" name="categoriesfilters">
        <label>Clima</label>
   </span>
</div>
<div class="chb_group">
    <span class="custom_chb_wrapper">
        <input class="zcheckbox" type="checkbox" value="166" name="categoriesfilters">
        <label>Range Command</label>
    </span>
</div>

这只是很长表格的一部分。我正在尝试遍历名称为 categoriesfilters 的复选框,并获取属于该组的选中复选框的值。

我正在尝试使用以下代码:

foreach ($_POST as $name=>$value) {
                    if($name == 'categoriesfilters') {
                    echo $name . " -> ".$value ."<br />";
                    }
                 }

但我只得到最后一个复选框,即使我检查了所有这些复选框。有人可以帮忙吗?

问候,约翰

【问题讨论】:

    标签: php loops checkbox


    【解决方案1】:

    您应该将输入的 name 更改为 name="categoriesfilters[]"

    然后您可以使用$_POST['categoriesfilters'] 访问categoriesfilters

    记住$_POST['categoriesfilters']是一个整数数组,所以:

    foreach ($_POST as $name=>$value) {
      if ($name == 'categoriesfilters') {
        echo $name . " -> ". implode(',', $value) ."<br />";
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2015-05-12
      • 2017-04-03
      • 2013-09-12
      • 2013-08-16
      • 1970-01-01
      相关资源
      最近更新 更多