亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讀取多個復選框(以及與之關聯的數據)?

如何讀取多個復選框(以及與之關聯的數據)?

蝴蝶不菲 2023-03-03 10:29:20
我有一個帶有多個復選框的表單,每個復選框作為與其關聯的輸入字段,一旦您選中該復選框就會激活。我的前端工作得很好,但我想知道如何在我的 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 中讀取該數組。但是,這并沒有增加每個具有輸入字段的復選框的復雜性。有誰知道如何做到這一點?
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

您希望對輸入和復選框都使用關聯數組。


<!-- 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]anditems[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"; 

}


查看完整回答
反對 回復 2023-03-03
  • 1 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號