Jump to content

Ajax - detecting Mysql Error in xmlhttp


abdfahim

Recommended Posts

Hi,

 

I am using Ajax to run a page where I update my database with current time and other dynamic thing. The problem is, no matter whether the mysql_query goes fine or not, readyState always returns 4 & status returns 200, though I use die on mysql_error .... So, how can I detect, from my ajax page, that the database update was successful or not?

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/216984-ajax-detecting-mysql-error-in-xmlhttp/
Share on other sites

Your ajax request isn't failing so checking your http status won't help you.

 

You need to send back an appropriate error message.

 

Hi,

 

I tried that. I have echo the word "success" at the end of the script. So, if everything goes fine, the responseText should return only success, if not then it will die somewhere and hence success will not be return.

start_session();
include("dbconnection.php");
mysql_query("SELECT INTO ........... ") or die('error'); 
// Here many query and other things
echo "success";

 

Then in my ajax function, I have written some thing like

if(xmlhttp.responseText == "success"){
document.getElementById("abcd").innerHTML = "success";
loadXML(2);
}else{
document.getElementById("abcd").innerHTML = "Error";
}

 

Unfortunately, this doesn't work! It always shows Error. I have test xmlhttp.responseText and it returns "success", but the ajax function evaluate the else statement (and shows "Error"). This is a real maze for me. If anyone can help, highly appreciate.

 

Thanks,

You might want to trim the returned value of whitespace.

 

if (xmlhttp.responseText.replace(/^\s*/, "").replace(/\s*$/, "") == "success") {
   document.getElementById("abcd").innerHTML = "success";
   loadXML(2);
}else{
   document.getElementById("abcd").innerHTML = "Error";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.