Checkbox Input for Line Items

I have a multi-line input form to allow the user to add as many line items as wished.  Each line has a checkbox to indicate whether that item is taxable.  I am unable to reliably receive the input.  Every option that I tried has failed to some degree.

forms/edit.php:

foreach($receipt_items as $item){
...
        if ($item->taxable == 1){
            $tax_check = elgg_view('input/checkbox', array(
                                            'name'    => 'item[taxable][]',
                                            'checked' => 'checked',
                                            'value'   => 1,
                                            'default' => false,
                                        ));
        } else {
            $tax_check = elgg_view('input/checkbox', array(
                                            'name'    => 'item[taxable][]',
                                            'value'   => 1,
                                            'default' => false,
                                        ));
        } 

...
echo"        <div class='rTableRow'>
                 <div class='rTableCell' style='width:5%'>".$tax_check."</div>
            </div>";
}

edit/942

The output for a taxable item:

                <div class='rTableCell' style='width:5%'><input type="checkbox" name="item[taxable][]" checked="checked" value="1" class="elgg-input-checkbox" />

The output for a non-taxable item:

                <div class='rTableCell' style='width:5%'><input type="checkbox" name="item[taxable][]" value="1" class="elgg-input-checkbox" />

In this example, the taxable checkbox is checked and is saved for the first item.  When one unchecks the taxable checkbox for the first line item, checks the taxable checkbox for the second line item and saves the form, the first item remains taxable and the second one remains untaxable; however, when checking both taxable check boxes, both items are (successfully) marked taxable.

actions/edit.php

$items     = get_input('item');
$input     = array();
$variables = elgg_get_config("{$subtype}");

foreach ($variables as $name => $type) {
    $input[$name] = get_input($name);
}

foreach($input as $key => $value){
        $jot->$key = $value;
      }

  // Process line items
      // Pivot Line Items
      foreach ($items as $key=>$values){
          foreach($values as $key_value=>$value){
              $line_items[$key_value][$key] = $value;
          }
      }

      foreach ($line_items as $line => $values) {
        foreach($values as $dimension => $value){
            $line_item->$dimension = $value;
        }
        if ($line_item->save()){
        }

The action actually receives the wrong values from the form.  To state the obvious:  I want to save the taxable value to the taxable item.  What is the trick to passing the correct value for a checkbox?

Thank you for your help!

  • I still haven't figured this out.  Any insight, even directionally, would be greatly appreciated.

  • I'm not sure if multi-dimensional arrays (item[taxable][])) can be used as name value in inputs at all. If it wouldn't work indeed, you would have to add some kind of counter to the names, e.g.

    $i = 0;
    $tax_check = '';
    foreach($receipt_items as $item){
        if ($item->taxable == 1){
            $tax_check .= elgg_view('input/checkbox', array(
                'name'        => "item_taxable_{$i}",
                'checked'    => 'checked',
                'value'        => 1,
                'default'    => false,
            ));
        } else {
            $tax_check .= elgg_view('input/checkbox', array(
                'name'        => "item_taxable_{$i}",
                'value'        => 1,
                'default'    => false,
            ));
        }
        i++;
    }

    Then you would also add a hidden input for the action to receive the number of items. Within the action you can then loop over the number of items and receive each item's value.

    You also need to append the html content to $tax_check within the foreach loop. Otherwise, you won't get the full list of item checkboxes but only the last one.

    I would suggest to concentrate first for the action getting the correct values from the form - maybe with a single input field at first. If this works you can continue with enhancing the form and action to handle default values and to show the previously selected values.

  • Thanks iionly.  I'll give sequential numbering a go.  For the record, multi-dimensional arrays do work for other line item elements on the form (qty, name, cost, etc.).  I can add as many lines as I want and it works.  The action does receive the input, it's just the wrong input for the checkbox.  My hunch says it's related to either the default or to each checkbox having the same (as in 'not unique to that line item') value, that being 1, if checked. 

  • Not long ago I also had some trouble with the checkbox input view when I wanted to add a new checkbox input. I also found the "checked", "value" and "default" attributes confusing to use / not working as I thought at first they would. Unfortunately, I don't remember at the moment which plugin it was that I added the checkbox to for reference.

    It might be worth trying without setting the "default" attribute at all. Or setting the default to 0 instead of false (or value to true instead of 1 as it might be an issue of variable type mixup).

    It might also work without the if-clause:

    $tax_check .= echo elgg_view('input/checkbox', array(
        'name'        => "item_taxable_{$i}",
        'value' => true,
        'checked' => (bool)$item->taxable,
    ));

    (And if multi-dimensional arrays work and it's only a matter of displaying / retrieving the correct state of the checkbox you could also return to your original name).