I have been sitting with this project for 2 days and couldn’t figure out what is wrong. I Will be grateful if u could help me point out the problem
- I want to delete a record from the HTML table. I can retrieve the book_id, how can I access the book_id in my delete function.
<script>
fetch('http://localhost:3003/booked').then(function (response) {
return response.json();
}).then(function (data) {
appendData(data);
}).catch(function (err) {
console.log("error:" + err);
});
function appendData(data) {
let tbodyContainer = document.getElementById("tbody");
for (let i = 0; i < data.length; i++) {
let tr = document.createElement("tr");
tr.innerHTML =
"<tr>"
+
"<th>" + data[i].book_id + "</th>" +
"<th>" + data[i].descr + "</th>" +
"<th>" + data[i].book_date + "</th>" +
`<th> <a href='/booked/${data[i].book_id}'>Cancel</a></th>` +
"</tr>";
tbodyContainer.appendChild(tr);
}
}
</script>
- How can I retrieve the book_id from the HTML table and pass it to the delete function below, so that I can compare the book_id and delete that record. When I console.log the book_id, it returns
undefined
app.delete("/booked/:book_id", (req, res) => {
const book_id = req.params.book_id;
console.log("--------------> " + book_id);
if (!req.session.loggedin) {
return res.status(401).redirect("./login");
} else {
connection.query("DELETE FROM calendar.book WHERE book_id= '" + book_id + "'", function (err, result) {
if (err) {
throw err;
} else {
res.redirect("./myBookings");
}
});
}
});