-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
122 lines (104 loc) · 4.43 KB
/
action.php
File metadata and controls
122 lines (104 loc) · 4.43 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
session_start();
include('hms/include/config.php');
// Add to cart
if (isset($_POST['pcode'], $_POST['pname'], $_POST['pimage'], $_POST['pprice'], $_POST['pdiscount'])) {
$code = trim($_POST['pcode']);
$name = trim($_POST['pname']);
$image = trim($_POST['pimage']);
$price = floatval($_POST['pprice']);
$discount = floatval($_POST['pdiscount']);
$qty = 1;
$total = $discount * $qty;
$stmt = $con->prepare("SELECT product_code FROM cart WHERE product_code = ?");
$stmt->bind_param("s", $code);
$stmt->execute();
$result = $stmt->get_result();
if (!$result->num_rows) {
$stmt = $con->prepare("INSERT INTO cart (proname, proprice, prodiscount, proimg, quantity, total_price, product_code) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sddsiis", $name, $price, $discount, $image, $qty, $total, $code);
if ($stmt->execute()) {
echo "<div class='alert alert-success'>Item added to your cart</div>";
} else {
echo "<div class='alert alert-danger'>Something went wrong. Try again.</div>";
}
} else {
echo "<div class='alert alert-warning'>Item already in your cart</div>";
}
}
// Cart item count for badge
if (isset($_GET['cartItem']) && $_GET['cartItem'] === 'cart_item') {
$result = mysqli_query($con, "SELECT COUNT(*) AS count FROM cart");
$row = mysqli_fetch_assoc($result);
echo $row['count'] ?? 0;
}
// Remove single item
if (isset($_GET['remove'])) {
$id = intval($_GET['remove']);
$stmt = $con->prepare("DELETE FROM cart WHERE cart_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$_SESSION["showAlert"] = "block";
$_SESSION["message"] = "Item removed from cart";
header("Location: cart.php");
exit;
}
// Clear entire cart
if (isset($_GET['clear'])) {
mysqli_query($con, "DELETE FROM cart");
$_SESSION["showAlert"] = "block";
$_SESSION["message"] = "All items removed from the cart";
header("Location: cart.php");
exit;
}
// Update quantity
if (isset($_POST['pqty'], $_POST['pid'], $_POST['pprice'])) {
$qty = max(1, intval($_POST['pqty']));
$id = intval($_POST['pid']);
$price = floatval($_POST['pprice']);
$total = $qty * $price;
$stmt = $con->prepare("UPDATE cart SET quantity = ?, total_price = ? WHERE cart_id = ?");
$stmt->bind_param("idi", $qty, $total, $id);
$stmt->execute();
}
// Place order
if (isset($_POST['action']) && $_POST['action'] === 'order') {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$address = trim($_POST['address']);
$pmode = trim($_POST['pmode']);
$products = trim($_POST['products']);
$grand_total = floatval($_POST['grand_total']);
$testid = trim($_POST['testid']);
$pid = trim($_POST['pid']);
// Basic validation
if (empty($name) || empty($email) || empty($phone) || empty($address) || empty($pmode) || empty($products) || empty($grand_total)) {
echo "<div class='alert alert-danger'>Missing required fields.</div>";
exit;
}
$stmt = $con->prepare("INSERT INTO orders (name, email, phone, address, payment_mode, products,pro_id, paid_amount) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssd", $name, $email, $phone, $address, $pmode, $products, $pid, $grand_total);
if ($stmt->execute()) {
// Clean up cart if testid is valid
if (!empty($testid)) {
$idList = implode(',', array_map('intval', explode(',', $testid)));
mysqli_query($con, "DELETE FROM cart WHERE cart_id IN ($idList)");
}
// Confirmation message
echo '<div class="text-center animated-message">
<h1><i class="fa fa-check-circle" aria-hidden="true"></i> Thank You!</h1>
<h2>Your order is successfully placed!</h2>
<h4 class="text-center">Item Purchased: ' . htmlspecialchars($products) . '</h4>
<h4>Your name: ' . htmlspecialchars($name) . '</h4>
<h4>Your email: ' . htmlspecialchars($email) . '</h4>
<h4>Your Phone: ' . htmlspecialchars($phone) . '</h4>
<h4>Total Amount Paid: Rs. ' . number_format($grand_total, 2) . '</h4>
<h4>Payment mode: ' . htmlspecialchars($pmode) . '</h4>
</div>';
} else {
echo "<div class='alert alert-danger'>Failed to place order. Please try again.</div>";
}
}
?>
<!-- action.php file -->