0

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

3
  • 1
    why do you tag jquery to this question? Commented Aug 11, 2015 at 11:29
  • 2
    jquery is used to dynamically add the inputs to the dom, and i would use it to send a number if i need to index the array @Jai Commented Aug 11, 2015 at 11:35
  • 2
    yeah, you need to add indexes to each set of elements units[0], article_group[0] etc. Commented Aug 11, 2015 at 11:36

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

I changed my ajax query to add indexes, and that worked, thanks!
I noticed a problem, that maybe you have a solution for. I can remove input rows with the delete button, and this breaks the iteration because the count is something like 3, but i have [0],[1],[3],[4],[6]. This gives me an illegal offset [2] error. Thanks
Which solution did you choose? In your PHP code, you could do foreach($items['units'] as $i => $unit) and use the $i as index
Yes, but then it would loop through $items['units'] first, and i need to save every input with the same index as a row in the DB. Or am i missing sonething? I did like you and @billyonecan, and added indexes to the inputs
When you simply replace your for line with this one: foreach(array_keys($items['units']) as $i): the $i (the key) will only have the actually affected values (ie the one you need).
0

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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.