abdfahim Posted October 27, 2010 Share Posted October 27, 2010 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, Quote Link to comment https://forums.phpfreaks.com/topic/216984-ajax-detecting-mysql-error-in-xmlhttp/ Share on other sites More sharing options...
trq Posted October 27, 2010 Share Posted October 27, 2010 Your ajax request isn't failing so checking your http status won't help you. You need to send back an appropriate error message. Quote Link to comment https://forums.phpfreaks.com/topic/216984-ajax-detecting-mysql-error-in-xmlhttp/#findComment-1127043 Share on other sites More sharing options...
abdfahim Posted October 27, 2010 Author Share Posted October 27, 2010 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, Quote Link to comment https://forums.phpfreaks.com/topic/216984-ajax-detecting-mysql-error-in-xmlhttp/#findComment-1127090 Share on other sites More sharing options...
trq Posted October 27, 2010 Share Posted October 27, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/216984-ajax-detecting-mysql-error-in-xmlhttp/#findComment-1127229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.