[英]Posting all checkboxes in form array
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. 这些输入作为行动态添加(使用Jquery),可以命名为一个,也可以为10、20。
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 问题是,当我将这些提交到php时,我无法知道哪些行的“发票”和“ D.note”复选框处于选中状态或未选中状态,因为我正在接收的数组看起来像这样
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. 但实际上,选中的是行“ 0”和“ 2”,而未选中的是行“ 1”。
This is how i iterate through it in php/laravel 这就是我在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 因为我使用没有设置索引的数组,所以具有同名的隐藏输入将无法工作,也许我可以让我的ajax调用发送一个数字来索引数组,但是必须有一个更简单的解决方案
Or does anyone have a suggestion for another solution? 还是有人对其他解决方案有建议?
Regards Johan 问候约翰
I see some possibilities to solve this issue with JavaScript: 我看到使用JavaScript解决此问题的一些可能性:
name="delivery_note[]"
, you create an enumeration : name="delivery_note_1"
(and in your PHP code, you loop until they don't exist) 要么不使用name="delivery_note[]"
,而是创建一个枚举: name="delivery_note_1"
(在您的PHP代码中,循环播放直到它们不存在为止) name="delivery_note[1]"
(solution used by the asker) 或@billyonecan建议使用name="delivery_note[1]"
( 询问者 使用的解决方案) You can assign a unique value to each checkbox. 您可以为每个复选框分配唯一值。 Then you can use the value to identify the checkbox 然后,您可以使用该值来标识复选框
Checker: 检查器:
<?php
foreach($_POST['check'] as $k){
// Value of checkbox that are checked
echo $k;
}
?>
Form: 形成:
<form method='POST'>
<input type='checkbox' name='check[]' value='1'>
<input type='checkbox' name='check[]' value='2'>
<input type='checkbox' name='check[]' value='3'>
<input type='checkbox' name='check[]' value='4'>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.