Reputation: 411
Regardless of what I type in my edit_data.php
file, I always get a success message. I can't setup error or, in this case, success handling as it always says my variables are undefined.
How do I setup error handling if all my variables keep erroring out with undefined?
function UpdateDB() {
// DEBUG
alert('UPDATED DB!');
//Hide All Alerts
$('.alert').hide();
//Show processing message.
$('#processing_alert').fadeIn(1000);
// Run MySQL Script
$.ajax({
url: 'edit_data.php',
success: function(data, status, error) {
// IF SUCCESSFUL
//Hide All Alerts
$('.alert').hide();
//Show processing message.
$('#databaseS_alert').fadeIn(1000);
$('#generated_msg_placehold').after('<h4>Original Request: ' + $data + '</h4>');
$('#generated_msg_placehold').after('<h4>Status: ' + $status + '</h4>');
$('#generated_msg_placehold').after('<h4>Error: ' + $error + '</h4>');
},
error: function(data, status, error) {
// IF FAILED
//Hide All Alerts
$('.alert').hide();
//Show processing message.
$('#databaseF_alert').fadeIn(1000);
$('#generated_msg_placehold').after('<h4>Original Request: ' + $data + '</h4>');
$('#generated_msg_placehold').after('<h4>Status: ' + $status + '</h4>');
$('#generated_msg_placehold').after('<h4>Error: ' + $error + '</h4>');
}
});
}
Any suggestions?
Upvotes: 0
Views: 707
Reputation: 71422
The success/fail from the AJAX standpoint is based on the HTTP response received for the request. A 200 response is a success. So even if you PHP script sent out a message like Epic fail
, unless you also modify the headers to also return some sort of error header (i.e. 4XX or 5XX response code), you will always engage the success handler.
Upvotes: 0
Reputation: 15929
You don't need dollar signs before variables in Javascript. That's probably why they're all coming up as undefined. Should be,
success: function(data, status, error) {
// IF SUCCESSFUL
//Hide All Alerts
$('.alert').hide();
//Show processing message.
$('#databaseS_alert').fadeIn(1000);
$('#generated_msg_placehold').after('<h4>Original Request: ' + data + '</h4>');
$('#generated_msg_placehold').after('<h4>Status: ' + status + '</h4>');
$('#generated_msg_placehold').after('<h4>Error: ' + error + '</h4>');
},
error: function(data, status, error) {
// IF FAILED
//Hide All Alerts
$('.alert').hide();
//Show processing message.
$('#databaseF_alert').fadeIn(1000);
$('#generated_msg_placehold').after('<h4>Original Request: ' + data + '</h4>');
$('#generated_msg_placehold').after('<h4>Status: ' + status + '</h4>');
$('#generated_msg_placehold').after('<h4>Error: ' + error + '</h4>');
}
Upvotes: 4