Hi I have a shopping cart but my problem is when I delete multiple Item it wont process but if I delete only one item on check box it work well here is my code below I want to figure out if what is wrong on my code:
$cart = $_SESSION['items'];
if(isset($_POST['del'])) {
if ($cart) {
$newcart = array();
foreach ($cart as $item) {
foreach ($_POST['itemid'] as $del) {
$delete = $del;
}
if ($del != $item) {
if ($newcart != '') {
$newcart[] = $item;
} else {
$newcart[] = $item;
}
}
}
$cart = $newcart;
}
}
$_SESSION['items'] = $cart;
Here is my form:
<form action="cart.php" method="post">
<table>
<tr>
<td><input type="checkbox" name="itemid[]" value="1"></td><td>Item 1</td><td>5</td>
</tr>
<tr>
<td><input type="checkbox" name="itemid[]" value="2"></td><td>Item 2</td><td>5</td>
</tr>
<tr>
<td><input type="checkbox" name="itemid[]" value="3"></td><td>Item 3</td><td>5</td>
</tr>
<tr>
<td><input type="checkbox" name="itemid[]" value="4"></td><td>Item 4</td><td>5</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="del" value="Delete"></td>
</tr>
</table>
</form>
If I check multiple Item and submit the delete button, the item was not delete but if I only check one the item deleted.
Thank you for you help very much appreciated.
You have name=”items” in the code, but you’re looking for $_POST[‘itemid’] – those should be the same as each other? Looks like you’re getting confused with the sessions value.
Post your edit:
will leave only the LAST items as $delete. you could try