dimanche 7 janvier 2018

How to send all checkboxes whether checked or unchecked on form submit?

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 Controllerit 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




Aucun commentaire:

Enregistrer un commentaire