I am having trouble with my javascript. It seems to be acting oddly. This is what\'s going on. I have a form, after the user submits it, it calls a function(onsubmit event)
The problem is that XMLHTTPRequest is asynchronous - it sends the request in the background and doesn't wait for it to finish.
The alert
statement causes the code to wait until the user clicks OK, during which the request finishes.
You need to use the onreadystatechange
event, like this:
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4) {
// Do things
}
};
The method you assign to this property will be called after the response is received. (and at other times, which is why you need to check that readyState
is 4)