0

I am having a issue in getting the correct value from the table.

The issue I encountered:

  • When I choose the second checkbox the and input a quantity, it is Undefined offset: 0

But the value of the checkbox is working and correct.

What I am expecting is when I choose the second or other checkbox (exclude first checkbox), I should get the value of that input field.

HTML

<?php foreach($results as $row) { ?>
<tr>
    <td><input type="checkbox" name="products[]" value="<?php echo $row->items_id; ?>"></td>
    <td><?php echo $row->item_name; ?></td>
    <td><input type="number" name="quantity[]" class="form-control"></td>
</tr>
<?php } ?>
<input type="submit" name="process" class="btn btn-primary" value="Process">

PHP

$quantity = $_POST['quantity'];
$products = $_POST['products'];
$count_selected = count($products);

for($i=0; $i< $count_selected; $i++){
    var_dump($quantity[$i]); exit;
}

sample image of the view

Emjey23
  • 339
  • 1
  • 4
  • 16
  • what is the output of `var_dump($quantity)` ? – qskane Apr 26 '18 at 05:18
  • `$row->items_id`? Is that correct? You sure it's not `$row->item_id`? – Phil Apr 26 '18 at 05:19
  • The problem with checkboxes is that an unchecked one submits no value so your `$_POST['products']` and `$_POST['quantity']` arrays may have different lengths. – Phil Apr 26 '18 at 05:20
  • @qskane, it has an error `Undefined offset: 0` below to that error it has a value of `NULL` – Emjey23 Apr 26 '18 at 05:22
  • @Phil, variable in `HTML` is correct. As you said that, it may the main issue. Can you give some idea on how I can get the value of the table row if the specific checkbox is selected? – Emjey23 Apr 26 '18 at 05:25

2 Answers2

1

The problem with checkboxes is that an unchecked one submits no value so your $_POST['products'] and $_POST['quantity'] arrays may have different lengths.

I'd combine using a hidden input with specific array indexes.

For example

<?php foreach($results as $row) : ?>
<tr>
    <td>
        <input type="hidden" name="products[<?= $row->items_id ?>]" value="0">
        <input type="checkbox" name="products[<?= $row->items_id ?>]" value="1">
    </td>
    <td><?= htmlspecialchars($row->item_name) ?></td>
    <td><input type="number" name="quantity[<?= $row->items_id ?>]" class="form-control"></td>
</tr>
<?php endforeach ?>

Then the arrays will have the same indexes and you can iterate them with a foreach

foreach ($_POST['products'] as $itemId => $checked) {
    // $checked represents the state of the checkbox
    // you can access quantity via $_POST['quantity'][$itemId]
}

You could even create a nice filtered array of quantities via

$selections = $_POST['products'];
$quantities = array_filter($_POST['quantity', function($itemId) use ($selections) {
    return $selections[$itemId];
}, ARRAY_FILTER_USE_KEY);
Phil
  • 157,677
  • 23
  • 242
  • 245
  • I will try what you suggest. As soon I get the correct answer related to what you answer, I will get back to you on this. I really appreciate it. Thank. – Emjey23 Apr 26 '18 at 05:39
  • Sorry took too long, anyway I got it working by following your answer. Thanks. – Emjey23 Apr 26 '18 at 07:32
-1

first, define array variable. $products =array( ); $quantity=array( ); other wise every thing seems fine.

  • OP already has defined `$products` and `$quantity`. Why would they need to first define them as empty arrays? – Phil Apr 26 '18 at 05:35