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

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

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

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>