Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit d2a91a26 authored by 65160270's avatar 65160270
Browse files

update-server

parent 02318cae
No related branches found
No related tags found
No related merge requests found
......@@ -62,6 +62,27 @@ app.get('/order/checkout', isLoggedIn, (req, res) => {
res.render('checkout');
});
app.post("/cart/update", async (req, res) => {
try {
const { cartItemId, quantity } = req.body;
if (quantity < 1) {
return res.status(400).json({ message: "Quantity must be at least 1" });
}
// อัปเดตจำนวนสินค้าในฐานข้อมูล
await pool.execute(
"UPDATE cart SET quantity = ? WHERE id = ?",
[quantity, cartItemId]
);
res.json({ success: true, message: "Cart updated" });
} catch (error) {
console.error("Update error:", error);
res.status(500).json({ message: "Update failed" });
}
});
app.get('/register', (req, res) => {
res.render('register');
});
......
......@@ -63,17 +63,23 @@
</div>
<script>
document.querySelectorAll('.edit-btn').forEach(button => {
button.addEventListener('click', (event) => {
const cartItem = event.target.closest('.cart-item');
const editButton = cartItem.querySelector('.edit-btn');
const removeForm = cartItem.querySelector('.remove-form');
document.querySelectorAll(".update-btn").forEach(button => {
button.addEventListener("click", async (event) => {
const cartItem = event.target.closest(".cart-item");
const cartItemId = cartItem.dataset.id;
const quantity = cartItem.querySelector("input[name='quantity']").value;
// ซ่อนปุ่ม Edit
editButton.style.display = 'none';
const response = await fetch('/cart/update', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cartItemId, quantity })
});
// แสดงปุ่ม Remove
removeForm.style.display = 'block';
if (response.ok) {
window.location.reload(); // รีเฟรชหน้าหลังอัปเดต
} else {
alert("Failed to update cart.");
}
});
});
</script>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment