Skip to content Skip to sidebar Skip to footer

Working With Checkbox From Form Array In Php

Am looping products out from product table to add them to cart table. only the selected product by checking the checkbox should be added, but if you select, the selected ones do no

Solution 1:

I used to do this the same way in the past, and have discovered what I think is a better way. Rather than having a hidden input that stores the ID, just use the ID as the index for all of the form variable keys:

<inputtype="checkbox"name="slected[<?phpecho$ID; ?>]"class="checkboxes"value="1" /><inputtype="text"name="name[<?phpecho$ID; ?>]"class=""value="<?phpecho$NAME;?>" />

Then, your PHP can be simplified:

// now when we submot the form$slected = $_POST['slected'];
$name = $_POST['name'];

foreach( (array)$slectedas$ID => $on ) {
    echo$product . ' ' . $name[$ID] . ' ' . $ID . '--<br>';
}

So - basically, your $slected variable will contain an array of only items that are selected, AND you have the product ID built in to the $slected array.

Post a Comment for "Working With Checkbox From Form Array In Php"