如何发送所有复选框,无论选中或未选中表单提交? [英] How to send all checkboxes whether checked or unchecked on form submit?
问题描述
我有一个带有多个复选框的表单
< input type =checkboxname =group_checkbox []value =1/>
< input type =checkboxname =group_checkbox []value =1/>
< input type =checkboxname =group_checkbox []value =1/>
< input type =checkboxname =group_checkbox []value =1/>
< input type =checkboxname =group_checkbox []value =1/>
所以当我点击提交按钮来提交表单时,它只会发送复选框。 p>
我在网上搜索并发现我们可以在真实复选框上面包含隐藏字段,如下所示:
< input type =hiddenname =group_checkbox []value =0/>
< input type =checkboxname =group_checkbox []value =1/>
< input type =hiddenname =group_checkbox []value =0/>
< input type =checkboxname =group_checkbox []value =1/>
< input type =hiddenname =group_checkbox []value =0/>
< input type =checkboxname =group_checkbox []value =1/>
< input type =hiddenname =group_checkbox []value =0/>
< input type =checkboxname =group_checkbox []value =1/>
< input type =hiddenname =group_checkbox []value =0/>
< input type =checkboxname =group_checkbox []value =1/>
但是当我做 var_dump($ request-> group_checkbox) code $>
Laravel Controller
它显示
array(10 ){[0] =>字符串(1)0[1] =>字符串(1)1[2] =>字符串(1)0[3] =>字符串(1)1[4] =>字符串(1)0[5] =>字符串(1)1[6] =>字符串(1)0[7] =>字符串(1)1[8] =>字符串(1)0[9] =>当所有5个复选框被选中时,字符串(1)1}
并显示
array(5){[0] =>字符串(1)0[1] =>字符串(1)0[2] =>字符串(1)0[3] =>字符串(1)0[4] =>字符串(1)0}
没有勾选复选框。 b
那么为什么当复选框被选中时,它会发送(0和1),而不仅仅是1而不是
这是PHP如何处理以[]结尾的参数的多个值。如果你想用选中的复选框覆盖0值,你需要显式地为索引命名,否则PHP会将其解释为应该附加到数组的值。
因此,使用 grouped_checkbox
或 grouped_checkbox [0]
。
像这样:
< input name =grouped_checkbox [0]type =hiddenvalue =0 >
< input name =grouped_checkbox [0]type =checkboxvalue =1> <! - 如果选中 - 这将覆盖前一个 - >
< input name =grouped_checkbox [1]type =hiddenvalue =0>
< input name =grouped_checkbox [1]type =checkboxvalue =1> <! - 如果选中 - 这将覆盖前一个 - >
<! - ...等等 - >
I have a form with multiple checkbox
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="checkbox" name="group_checkbox[]" value="1" />
So when i click the submit button in to submit the form it only sends the checked boxes.
I searched on the net and found that we can include hidden field above the real checkbox like so:
<input type="hidden" name="group_checkbox[]" value="0" />
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="hidden" name="group_checkbox[]" value="0" />
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="hidden" name="group_checkbox[]" value="0" />
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="hidden" name="group_checkbox[]" value="0" />
<input type="checkbox" name="group_checkbox[]" value="1" />
<input type="hidden" name="group_checkbox[]" value="0" />
<input type="checkbox" name="group_checkbox[]" value="1" />
But when i do var_dump($request->group_checkbox)
in Laravel Controller
it shows
array(10) { [0]=> string(1) "0" [1]=> string(1) "1" [2]=> string(1) "0" [3]=> string(1) "1" [4]=> string(1) "0" [5]=> string(1) "1" [6]=> string(1) "0" [7]=> string(1) "1" [8]=> string(1) "0" [9]=> string(1) "1" }
when all 5 checkboxes are checked.
and shows
array(5) { [0]=> string(1) "0" [1]=> string(1) "0" [2]=> string(1) "0" [3]=> string(1) "0" [4]=> string(1) "0" }
when no checkboxes are checked.
So why is it sending (0 and 1) when a checkbox is checked and not just 1 instead
This is how PHP handles multiple values for parameters ending in []. If you want to overwrite the 0 value with a checked checkbox, you will need to explicitly name the index, otherwise PHP interprets it as values that should be appended to the array.
So, use either grouped_checkbox
or grouped_checkbox[0]
.
Like this:
<input name="grouped_checkbox[0]" type="hidden" value="0">
<input name="grouped_checkbox[0]" type="checkbox" value="1"> <!-- this will overwrite the previous one, if checked -->
<input name="grouped_checkbox[1]" type="hidden" value="0">
<input name="grouped_checkbox[1]" type="checkbox" value="1"> <!-- this will overwrite the previous one, if checked -->
<!-- ... and so forth -->
这篇关于如何发送所有复选框,无论选中或未选中表单提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!