dqgg25493 2015-08-11 11:28
浏览 36
已采纳

在表单数组中发布所有复选框

Hi i have a form that looks like this:

<div class="row calc">
<div class="large 12 columns">
        <div class="large-1 columns">
        <label>Quantity</label>
        <input name="units[]" type="text" value="1">
    </div>
    <div class="large-1 columns">
        <label>GroupNR</label>
        <input name="article_group[]" type="text" value="2401">
    </div>
    <div class="large-2 columns">
        <label>Item name</label>
        <input name="article[]" type="text" value="Item 1">
    </div>
    <div class="large-3 columns">
        <label>Description</label>
        <input name="description_en[]" type="text" value="Description 1">
    </div>
    <div class="large-1 columns">
        <label>Price/Unit</label>
        <input name="unit_price[]" type="text" value="15500">
    </div>
    <div class="large-1 columns">
        <label>Discount</label>
        <input max="100" min="0" name="discount[]" type="number" value="0">
    </div>
    <div class="large-1 columns">
        <label>Invoice</label>
        <input checked="checked" name="invoice[]" type="checkbox">
    </div>
    <div class="large-1 columns">
        <label>D. Note</label>
        <input checked="checked" name="delivery_note[]" type="checkbox">
    </div>
    <div class="large-1 columns">
        <label>Delete</label>
        <a class="alert button tiny" onclick="$(this).closest('.calc').remove(); $( '#itemForm' ).trigger('change');">X</a>
    </div>

</div>

These inputs get added dynamically (with Jquery) as rows, and there could be one or there could be 10, 20 you name it.

The problem is that when i submit these to php, i have no way of knowing which rows have the "invoice" and "D.note" checkboxes checked or unchecked, because the array i'm recieving looks like this

 array:13 [▼
  "units" => array:3 [▼
    0 => "1"
    1 => "1"
    2 => "1"
  ]
  "article_group" => array:3 [▼
    0 => "2401"
    1 => "2201"
    2 => "2503"
  ]
  "article" => array:3 [▶]
  "description_en" => array:3 [▶]
  "unit_price" => array:3 [▶]
  "discount" => array:3 [▶]
  "invoice" => array:2 [▼
    0 => "on"
    1 => "on"
  ]
  "delivery_note" => array:2 [▼
    0 => "on"
    1 => "on"
  ]
]

But really, it is row "0" and "2" that are checked, and row "1" unchecked.

This is how i iterate through it in php/laravel

for($i=0;$i<count($items['units']);$i++){
        $bookdetails = new BookingDetails;
        $bookdetails->article_group = $items['article_group'][$i];
        $bookdetails->article = $items['article'][$i];
        $bookdetails->description_en = $items['description_en'][$i];
        $bookdetails->units = $items['units'][$i];
        $bookdetails->unit_price = $items['unit_price'][$i];
        $bookdetails->discount = $items['discount'][$i];
        $bookdetails->show_invoice = $items['invoice'][$i];
        $bookdetails->show_picklist = $items['delivery_note'][$i];
        $bookdetails->save();
    }

So is there a way to submit the unchecked boxes aswell? Having a hidden input with the same name wont work since i use array without set index, maybe i can make my ajax call send a number to index the array, but there has to be a easier solution

Or does anyone have a suggestion for another solution?

Regards Johan

  • 写回答

2条回答 默认 最新

  • dqyitt2954 2015-08-11 11:36
    关注

    I see some possibilities to solve this issue with JavaScript:

    • either instead of using name="delivery_note[]", you create an enumeration : name="delivery_note_1" (and in your PHP code, you loop until they don't exist)
    • or as @billyonecan suggests name="delivery_note[1]" (solution used by the asker)
    • or onsubmit (JS event, that you can easily capture with jquery) you do some treatment to insert in the checkbox array some other (default) values for the unchecked checkboxes
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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