Steve Tinsley
asked on
ajax error handling
I have created an ajax call which submits some php code to a mysql db and it works ok.
I now want to add some error handling so if the mysql query fails then the user gets told so.
The code below is how far I have got with it but the array always comes back undefined.
Could someone take a look and perhaps give me a clue why it doesn't work.
Many Thanks
Steve
javascript
submit php
I now want to add some error handling so if the mysql query fails then the user gets told so.
The code below is how far I have got with it but the array always comes back undefined.
Could someone take a look and perhaps give me a clue why it doesn't work.
Many Thanks
Steve
javascript
function saveCanvas(frm) {
	
$.blockUI({ message: '<h1><img src="images/busy.gif" /><br>Submitting...<br>Please Wait...</h1>' });
	
var strImageData = canvas.toDataURL();
	
$.ajax({
	type: 'POST',
	url: 'draw/submitimage.php',
	 	dataType : 'html',
		data: frm.serialize() + "&imageData="+encodeURIComponent(strImageData),
		cache: false,
		async: true,
		success: function(data) {
			if (data.status() == "success") {
				alert("SUCCESS. " + data.message);
			} else {
				alert("Oops. " + data.message);
			}
		},
		
		error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
	});
	
	 return false;
};
submit php
<?php require_once '../includes/config.php'; ?>
<?php
	 $ajaxResponse=array();
	 //convert all the posts to variables:
	 $terminalNumber = $_COOKIE['terminalNumber'];
	 $submissionType = "image";
	 $name = $_POST['name'];
	 $subject = $_POST['subject'];
	 $message = $_POST['description'];	
	 $imageData = $_POST['imageData'];
	
//Insert the values into the correct database with the right fields
$result=mysql_query("INSERT INTO messages (dateTime, terminalNumber, submissionType, name, subject, message, imageData)".
"VALUES (NOW(), '$terminalNumber', '$submissionType', '$name', '$subject', '$message', '$imageData')");	
	
	//handle your query.
if ($result) {
$ajaxResponse['status'] = "success";
$ajaxResponse['message'] = "Your feedback has been submitted";
} else {
$ajaxResponse['status'] = "failure";
$ajaxResponse['message'] = "There was a problem: " . mysql_error();
}
echo json_encode($ajaxResponse);
?>
In MySQL datetime is a reserved word. Not sure if that is the problem or not, but it could be a risk.
What happens if you remove the AJAX part of it and just make a form that posts directly to submit.php? If you try that it will help isolate the problem to the server side vs. the client side.
Also, please learn about this function before you get hacked.
http://us3.php.net/manual/en/function.mysql-real-escape-string.php
Also, please learn about this function before you get hacked.
http://us3.php.net/manual/en/function.mysql-real-escape-string.php
ASKER
Just so you know... even tho the alert that comes back is: Oops undefined it still successfully entering the record into the mysql db.
I could remove the code above and it all works fine. I would just like to inform the user if there was an error.
if (data.status() == "success") {
alert("SUCCESS. " + data.message);
} else {
alert("Oops. " + data.message);
}
I could remove the code above and it all works fine. I would just like to inform the user if there was an error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have altered the function so that all it does it alert("SUCCESS. " + data.message);
But it still alerts undefined.
Any other ideas?
But it still alerts undefined.
Any other ideas?
function saveCanvas(frm) {
$.blockUI({ message: '<h1><img src="images/busy.gif" /><br>Submitting...<br>Please Wait...</h1>' });
var strImageData = canvas.toDataURL();
$.ajax({
type: 'POST',
url: 'draw/submitimage.php',
dataType : 'html',
data: frm.serialize() + "&imageData="+encodeURIComponent(strImageData),
cache: false,
async: true,
success: function(data) {
alert("SUCCESS. " + data.message);
window.location.href = "index.php";
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
return false;
};
ASKER
Has anyone else got any ideas why the variable I am setting in the PHP script is showing as undefined in the javascript success statement?
ASKER
This was one of the problems.