I do see what you are saying about the code not stopping and throwing that error. How do I fix this? I added the “die;” code after each JSON echo. Is this not correct? Am I missing a step? This is my first time using AJAX and JSON in my own code. But I do see, and follow what is basically happening in it. Here are the updated snippets:
if($stmt->rowCount() > 0){
$error = "<br><span class='error_msg'>This e-mail has already been used! Contact me if you need assistance.</span><br>";
// Prepare the response
$response = array('error' => $error);
// Set the content type to JSON
header('Content-Type: application/json');
// Encode the response as JSON and echo it back to the client
echo json_encode($response);
die;
} else {
// Process the email sending and other tasks
// Prepare the success response
$response = array('success' => 'Thank you for your submission!');
// Set the content type to JSON
header('Content-Type: application/json');
// Encode the response as JSON and echo it back to the client
echo json_encode($response);
die;
}
...
The actual AJAX/JS:
$(document).ready(function(){
$("#register_form").submit(function(event){
event.preventDefault();
$("#loading").show();
$("body").css("background-color", "rgba(0, 0, 0, 0.5)"); // Dim the background
$.ajax({
type: "POST",
url: "buy_ajax.php",
data: $(this).serialize(),
dataType: "json", // Set the expected data type to JSON
success: function(response){
if (response.error) {
$("#error_msg").html(response.error);
$("#loading").hide();
$("body").css("background-color", ""); // Restore the background color
} else {
// Redirect to thanks.php or display a success message
window.location.href = "thanks.php";
}
},
error: function(xhr, status, error){
// Handle AJAX error
console.log("AJAX error: " + error);
$("#loading").hide();
$("body").css("background-color", ""); // Restore the background color
}
});
});
});
After looking at it line by line, it seems related to displaying the error message, or if success, redirect. The code works otherwise, so if the e-mail was used, I do see the spinner. It just doesn’t show the error message, and if the e-mail is new, I see the spinner for the 4 or 5 seconds while the code executes, and it adds the e-mail to the DB, and sends a message as intended (but no screen dimming). But again, it won’t redirect.
I’m also not sure why part of the code is:
// Prepare the success response
$response = array('success' => 'Thank you for your submission!');
But then later it is:
success: function(response){
if (response.error) {
$("#error_msg").html(response.error);
$("#loading").hide();
$("body").css("background-color", ""); // Restore the background color
} else {
// Redirect to thanks.php or display a success message
window.location.href = "thanks.php";
}
},
error: function(xhr, status, error){
// Handle AJAX error
console.log("AJAX error: " + error);
$("#loading").hide();
$("body").css("background-color", ""); // Restore the background color
}
The response string seems odd. One last thing: What’s up with this:
$("#error_msg").html(response.error);
“error_msg” is a class. So why is it $("#error_msg").html… Is it saying error_msg is an html file?