【问题标题】:How to read multiple checkboxes(and data associated with it)?如何读取多个复选框(以及与之关联的数据)?
【发布时间】:2021-01-01 22:04:29
【问题描述】:

我有一个带有多个复选框的表单,每个复选框都作为与之关联的输入字段,一旦您选中该复选框就会激活。

我的前端工作得很好,但我想知道如何在我的 PHP 中读取哪些复选框与它们各自的输入字段一起被选中(有些除了输入字段之外还有一个单选按钮)

这是我表单中每个复选框的样子:

 <!-- Option1 -->
              <div class="form-row">
                <div class="form-group col-md-8">
                  <div class="form-check">
                    <input class="form-check-input showman" type="checkbox" onchange="showqt()" value="Button Chicken Amritsari" id="c1">
                    <label class="form-check-label" for="c1">
                      Button Chicken Amritsari<i><br>(Boneless) Serves 2<br>INR 290</i>
                    </label>
                  </div>
                </div>
                  <div class="form-group col-md-4" id="ifYes" style="display: none; float:right;">
                    <!-- <label for="qtcounter">Quantity:</label> -->
                    <div class="input-group" id="qtcounter">
                      <input type="button" value="-" class="button-minus" data-field="quantity">
                      <input type="number" step="1" max="" value="1" name="quantity" class="quantity-field">
                      <input type="button" value="+" class="button-plus" data-field="quantity">
                    </div>
                  </div>
              </div>
              <!-- Option 1 ends -->

我还没有完全了解 PHP,但之前为了读取多个复选框,我将它们全部放在 name 字段中的一个数组中,然后在 PHP 中读取该数组。但是,这并没有增加每个具有输入字段的复选框的复杂性。

有人知道怎么做吗?

【问题讨论】:

  • 可以用ajax吗?
  • 最好尽量避免@Swati
  • 我没有完整的上下文,但请确保您的示例包含在
    元素中。此外,表单值通过它们各自的 name 属性作为键发送到服务器,但我在您的复选框元素中看不到 name 属性。

标签: javascript php html forms checkbox


【解决方案1】:

您想为输入和复选框使用关联数组。

<!-- Option1 -->
<div class="form-row">
  <div class="form-group col-md-8">
    <div class="form-check">
      <input class="form-check-input showman" type="checkbox" name="items[1][chosen]" onchange="showqt()" value="Button Chicken Amritsari" id="c1">
      <label class="form-check-label" for="c1">
        Button Chicken Amritsari<i><br>(Boneless) Serves 2<br>INR 290</i>
      </label>
    </div>
  </div>
    <div class="form-group col-md-4" id="ifYes" style="display: none; float:right;">
      <!-- <label for="qtcounter">Quantity:</label> -->
      <div class="input-group" id="qtcounter">
        <input type="button" value="-" class="button-minus" data-field="items[1][quantity]">
        <input type="number" step="1" max="" value="1" name="items[1][quantity]" class="quantity-field">
        <input type="button" value="+" class="button-plus" data-field="items[1][quantity]">
      </div>
    </div>
</div>
<!-- Option 1 ends -->

对于选项 2,使用 items[2][chosen]items[2][quantity] 等。

注意必须指定索引,不能使用[]。否则,quantity 的索引将与所选项目不匹配。

在 php 中,您可以遍历项目并忽略未选择的项目。

foreach ($_POST['items'] as $item) {
    if (!isset($item['chosen'])) continue; // Skip items that aren't chosen.

    echo $item['quantity'] . ' ' . $item['chosen'] . "\n"; 
}

【讨论】:

  • 让我处理一下然后回复你
  • 这只会读取列表中的第一项。其他的即使经过检查也不会被阅读
  • @YaddyVirus 这个答案是正确的。我认为您忘记编辑其他项目的相关字段名称。 name="items[1][quantity]" 用于第一个复选框,name="items[2][quantity]" 用于第二个复选框等等......
  • @Akinak 我完全按照原样使用了答案。原来问题是别的东西
猜你喜欢
  • 2020-03-02
  • 1970-01-01
  • 2020-01-28
  • 2013-08-21
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 2014-09-13
相关资源
最近更新 更多