I have a button that has a data-id with a value of 1. I need to pass this id to getCarDetails.php during a fetch() call. The getCarDetails needs this id for a query. The php is working fine as I tested it on a normal submit form and returns the jSON I need. However, this script doesn't want to work. I've tried everything.
function loadCarDetails(event) {
const id = event.currentTarget.getAttribute("data-id");
const data = new FormData();
data.append("car_id", id);
fetch("getCarDetails.php", {
method: "POST",
data
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error ${response.status}`);
}
return response.json();
})
.then(carDetails => {
// ...use the car details here...
})
.catch(error => {
// ...handle/reject error here...
});
}
<div class="car-item">
<div class="row">
<p>Ford Fiesta</p>
</div>
<div class="row">
<button type="button" class="btn" data-id="1">View</button>
</div>
</div>