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
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,

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.