views:

28

answers:

2

I am using Ajax to post data to the server(PHP code) and update it. i am posting many data one after the other, but in between the Ajax post fails and dont return a readyState to 4. the code is as follows

function getHttpRequest()
{
   var request=false;
   if(window.XMLHttpRequest)
   {
       request=new XMLHttpRequest();
   }
   else if(window.ActiveXObject)
   {
       try
       {
           request=new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
           try
           {
               request=new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch(e)
           {
               request=false;
           }
       }
   }

   return request;
}

the code begins here..

function updateAnswer(){
var request=getHttpRequest();
request.open('post','addAnswer.php');
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send("answer="+ans);

if(request.readyState == 4)
{
    var response=request.responseText;
    document.getElementById("display").innerHTML=response;
}
}

i call this function to update answer in database but it donot return status=4 sometimes... please help

A: 

innerinnerHTML should be innerHTML. updateAnswer gets called each time readyState is changing from zero to four. Four is fully loaded, while those lesser are different loading stages.

Delan Azabani
sorry for that inner inner... :D...
Harish Kurup
should i use readstatechange event and write the code in separate function (on success)??
Harish Kurup
Yes. I didn't realise that you didn't attach `updateAnswer` to `onreadystatechange`. Do that first.
Delan Azabani
then also it is giving error, some are being updated but some fails....
Harish Kurup
A: 

Why not make life easier and use a framework like jQuery?

Also, can't you post all the data at once, in that way save a few roundtrips to the server?

baloo
Because using libraries for simple stuff like this is bad. http://azabani.com/51
Delan Azabani
yeah! i agree... libraries are good to do complex things easy...
Harish Kurup