-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage_cart4.php
59 lines (53 loc) · 1.95 KB
/
manage_cart4.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
session_start(); // Start the session
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// Add Item to Cart
if (isset($_POST['Add_To_Cart'])) {
if (isset($_SESSION['cart'])) {
// Check if the item is already in the cart
$myitems = array_column($_SESSION['cart'], 'Item_Name');
if (in_array($_POST['Item_Name'], $myitems)) {
echo "<script>
alert('Item already added');
window.location.href='index5.php';
</script>";
} else {
$count = count($_SESSION['cart']);
$_SESSION['cart'][$count] = array(
'Item_Name' => $_POST['Item_Name'],
'price' => $_POST['Price'], // Corrected the case here
'Quantity' => 1
);
echo "<script>
alert('Item added to cart');
window.location.href='index5.php';
</script>";
}
} else {
// If cart doesn't exist, create it
$_SESSION['cart'][0] = array(
'Item_Name' => $_POST['Item_Name'],
'price' => $_POST['Price'], // Corrected the case here
'Quantity' => 1
);
echo "<script>
alert('Item added to cart');
window.location.href='index5.php';
</script>";
}
}
if (isset($_POST['Remove'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if($value['Item_Name'] == $_POST['Item_Name']) {
unset($_SESSION['cart'][$key]);
$_SESSION['cart'] = array_values($_SESSION['cart']); // Re-index the array
echo "<script>
alert('Item Removed');
window.location.href='index5.php';
</script>";
break;
}
}
}
}
?>