-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducts.php
88 lines (80 loc) · 3.31 KB
/
products.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
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
<?php
// Include database connection
include_once 'connection/config.php'; // Update this path as needed
// Fetch books from the database
$sql = "SELECT * FROM books";
$result = $mysqli->query($sql);
if (!$result) {
die("Error fetching books: " . $mysqli->error);
}
session_start();
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Products</title>
<link rel="stylesheet" href="./css/products.css">
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.1/css/all.min.css">
</head>
<body>
<?php include 'header.php'; ?>
<div class="container">
<h1>Our Books</h1>
<div class="products-grid">
<?php while ($row = $result->fetch_assoc()): ?>
<div class="product-card">
<img src="bookspic/<?php echo htmlspecialchars($row['book_img']); ?>" alt="<?php echo htmlspecialchars($row['title']); ?>">
<h3><?php echo htmlspecialchars($row['title']); ?></h3>
<p>Author: <?php echo htmlspecialchars($row['author']); ?></p>
<p>Price: $<?php echo htmlspecialchars($row['price']); ?></p>
<div class="buttons">
<form action="view_details.php" method="GET" style="display: inline;">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<button type="submit">View Details</button>
</form>
<form class="add-to-cart-form" data-id="<?php echo $row['id']; ?>" style="display: inline;">
<button type="button">Add to Cart</button>
</form>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<!-- Popup for "Added to cart" -->
<div id="popup-message"><p>Added to cart</p></div>
<!-- JavaScript for Cart Functionality -->
<script>
document.querySelectorAll('.add-to-cart-form').forEach(form => {
form.addEventListener('click', function () {
const productId = this.dataset.id;
fetch('add_to_cart.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `product_id=${productId}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
const popup = document.getElementById('popup-message');
popup.style.display = 'block';
setTimeout(() => { popup.style.display = 'none'; }, 2000);
} else {
alert(data.message || 'Failed to add to cart.');
}
})
.catch(err => console.error('Error:', err));
});
});
</script>
<?php include 'footer.php'?>
</body>
</html>