A1Dan75 Posted November 3, 2009 Share Posted November 3, 2009 Hi, I'm using ajax to save data to a mysql database as per the following code, and experiencing intermitent problems: function save() { xmlhttp=GetXmlHttpObject(); var url="save.php"; var params="&id="+curRouteID; var params=params+"&name="+document.getElementById('rName').value; url=url+"?sid="+Math.random(); url=url+params; xmlhttp.onreadystatechange=stateChangedsave; xmlhttp.open("GET",url,true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", params.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(params); } function stateChangedsave() { if (xmlhttp.readyState==4){ if(xmlhttp.responseText=='OK'){ alert ('Route Saved'); }else{ mapviewer.addEventHandler( 'click', clickHandlerDraw ); alert ('Error - Not Saved: '+xmlhttp.responseText); } } } save.php just writes the "Name" and "ID" values into a database and then echoes "OK". This works fine most of the time, but after a while will randomly fail - nothing is returned in the xmlhttp.responseText and the php script does not do the db write. Note that if the db write was failing, I would get mysql error in the xmlhttp.responseText, and so see it in the 'Error - Not Saved' alert. Once the save function has failed once, it will not then work again until I refresh the page. In order to narrow down where the error is, I added the following on another button on my page: function ajaxtest() { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="ajaxtest.htm"; xmlhttp.onreadystatechange=stateChangedtest; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChangedtest() { if (xmlhttp.readyState==4) { alert(xmlhttp.responseText); } } ajaxtest.htm literally just says "Hello". So, of course, under normal circumstances I can hit the test button and get a "hello" alert, but once the save has failed, hitting the button gives me an empty alert. Fromt his, we can tell that there is no problem specific to the mysql or the save.php script. It's just that the GetXmlHttpObject ajax gubbins has packed in on my main page. It's almost like something has timed out... anybody got any ideas? Quote Link to comment 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.