File size: 1,073 Bytes
4dd8a19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Add item to cart
function addToCart(name, price) {
    fetch("/add_to_cart", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ name: name, price: price })
    })
    .then(response => response.json())
    .then(data => {
        alert(data.message); // Display message that item was added
    })
    .catch(error => console.error("Error:", error));
}

// Place order (if additional client-side logic is required)
function placeOrder() {
    const email = document.getElementById("email").value;
    if (!email) {
        alert("Please enter your email to place the order.");
        return;
    }
    fetch("/place_order", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ email: email })
    })
    .then(response => response.json())
    .then(data => {
        alert(data.message); // Display success message
        window.location.href = "/cart"; // Redirect to the cart page
    })
    .catch(error => console.error("Error:", error));
}