Reputation: 3323
Added header to the PHP code and the catch in the Ajax but still no joy.
I'm having trouble getting my head around handling MySQL errors when using Ajax to handle form submissions. I have the following code:
var url = "save.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#frmSurvey").serialize(), // serializes the form's elements.
success: function(jqXHR, textStatus, errorThrown){
$('.sequence-container div').hide().delay( 2000 );
$next.show().delay( 2000 );
},
error: function(jqXHR, textStatus, errorThrown){
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 503) {
alert('Error: ' + jqHXR.responseText);
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404] - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist.');
} else if (jqXHR.status == 500) {
alert('Internal Server Error. [500] - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (exception === 'timeout') {
alert('Time out error - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (exception === 'abort') {
alert('Ajax request aborted - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText + ' - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
}
}
});
This works fine and handles any erros such as pages missing etc.
However, I am completely stumped how to then handle/ display to the user any errors that might arise when actually submitting to the database.
Currently I've tried this:
if ($mysql_error!="") {
header('HTTP/1.1 503 Service Unavailable');
printf("Unexpected database error: %s\n", $mysql_error);
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
exit();
}
But as the page is not being shown (as it is being submitted by Ajax) the error message doesn't appear. I think I have to bring that error message back into the Ajax script to display it to the user (is that right?) - and I'm not sure how to do this.
Any pointers/ suggestions?
Upvotes: 1
Views: 5462
Reputation: 3691
I would move all your error logic from jQuery to PHP. You can respond with a simple JSON object that can hold the status
(success or error), code
(if needed), message
, and even data
, if you want to provide specific results.
For example, you make a request like this:
$.ajax({
type: 'POST',
url: url,
data: $("#frmSurvey").serialize(),
success: function(result){
var json = $.parseJSON(result);
if(json.response.status == 'success') {
// do something
} else {
// look at message or code to perform specific actions
}
}
});
Then in the PHP file processing this request, you build an array with all the aforementioned elements you need (status, code, message, etc). Ultimately, you'll echo
something like this:
$result = array(
'response' => array(
'status' => 'error',
'code' => '1', // whatever you want
'message' => 'Could not connect to the database.'
)
);
echo json_encode($result);
The $result
array will contain the relevant data based on the checks you make in PHP.
Hope this helps!
Upvotes: 3
Reputation: 856
Send a 503-Header
<?php
header('HTTP/1.1 503 Service Unavailable');
and your ajax-code should work.
Edit: It will use your fail-code and you can check in the function for the status-code 503.
jqXHR.status
will be 503 and
jqXHR.responseText
will contain your message.
Edit2: js.file
var url = "save.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#frmSurvey").serialize(), // serializes the form's elements.
success: function(jqXHR, textStatus, errorThrown){
if (jqXHR.status == 404) {
alert('Error: ' + jqHXR.responseText);
}else{
$('.sequence-container div').hide().delay( 2000 );
$next.show().delay( 2000 );
}
},
error: function(jqXHR, textStatus, errorThrown){
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404] - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist.');
} else if (jqXHR.status == 500) {
alert('Internal Server Error. [500] - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (errorThrown === 'parsererror') {
alert('Requested JSON parse failed - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (errorThrown === 'timeout') {
alert('Time out error - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else if (errorThrown === 'abort') {
alert('Ajax request aborted - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText + ' - Click \'OK\' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist');
}
}
});
save.php:
<?php
header('HTTP/1.1 503 Service Unavailable');
echo('hallo welt');
exit();
result:
Uncaught Error.
hallo welt - Click 'OK' and try to re-submit your responses - if this error box re-appears, call 01255 850051 and we will assist
Upvotes: 3