-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
61 lines (58 loc) · 2.09 KB
/
index.html
File metadata and controls
61 lines (58 loc) · 2.09 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
<!DOCTYPE html>
<html>
<head>
<title>Books List</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body class="container mt-4">
<h2>Library</h2>
<a href="add.html" class="btn btn-primary mb-3">Add Book</a>
<table class="table table-bordered" id="bookTable">
<thead>
<tr>
<th>Title</th><th>Author</th><th>Genre</th><th>Year</th><th>Quantity</th><th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
function loadBooks() {
fetch('http://localhost:8000/library_project/books.php')
.then(res => res.json())
.then(data => {
const tbody = document.querySelector("#bookTable tbody");
tbody.innerHTML = '';
data.forEach(b => {
tbody.innerHTML += `
<tr data-id="${b.id}">
<td>${b.book_title}</td>
<td>${b.author_name}</td>
<td>${b.genre}</td>
<td>${b.publication_year}</td>
<td>${b.quantity}</td>
<td>
<a href="view.html?id=${b.id}" class="btn btn-info btn-sm">View</a>
<a href="edit.html?id=${b.id}" class="btn btn-warning btn-sm">Edit</a>
<button class="btn btn-danger btn-sm delete-btn" data-id="${b.id}">Delete</button>
</td>
</tr>`;
});
document.querySelectorAll('.delete-btn').forEach(btn => {
btn.addEventListener('click', function () {
if (confirm("Are you sure you want to delete this book?")) {
const id = this.getAttribute('data-id');
fetch(`http://localhost:8000/library_project/books.php?id=${id}`, { method: 'DELETE' })
.then(res => res.json())
.then(() => {
alert("Book deleted.");
loadBooks();
});
}
});
});
});
}
loadBooks();
</script>
</body>
</html>