I have a shopping cart page and button like
`<button class="purchase-button" data-courses-id="<?php echo $coursesID; ?>" onclick="Payment()">
</button>`
so when user click on it, the payment form will show and I want to get the ID of the product to display. This is the js file:
`
function Payment() {
document.querySelector(".body-payment").style.display = "flex";
document.body.style.overflow = "hidden";
document.querySelectorAll(".purchase-button").forEach((button) => {
button.addEventListener("click", (event) => {
const courseID = button.getAttribute("data-courses-id");
const data = { courseID: courseID };
$.post("payment.php", data)
.done(function (response) {
console.log("Payment data sent successfully:", data);
})
.fail(function (error) {
console.error("Error sending payment data:", error);
});
});
});
}
`
The ajax work fine, it send data, but PHP dont:
`
if (isset($_POST['courseID'])) {}
else echo "Error";
`
I try print the SERVER and POST like:
`
print_r($POST);
`
and the output I got is array empty.
I guess that I dont have a form tag, and maybe the button is clicked twice. Please help me!
Maybe the ajax dont really send data to PHP...if you use your browser's Network tool, you can see the AJAX request (you must open the tool before you send the request, though). You can click on the ajax request, and see the Payload data, so you can confirm exactly what it sends to PHP. You can also click on the Response data in the same area, to see exactly what PHP sends back in response to that particular request. (So actually you don't even need the console to debug it really)$POSTdoes probably not exist at all, it should be$_POST...is shown in the payment form (payment.php file)...so you mean, in the browser window and not in the console or network tool? Well in that case it sounds like it didn't come from an ajax request at all. Which would explain why it doesn't contain the data AJAX was sending. Have you looked in your network tool yet to see what's going on? Are you ever seeing thatconsole.log("Payment data sent successfully:", data);message in the console, or not? I must say there are a lot of comments here asking for info and you are replying with relatively vague information, which isn't helping anyone.