problem with xmlhttp calls

Discussion in 'JavaScript' started by Corol, Mar 10, 2010.

  1. #1
    	var xmlhttp = new getXMLObject();
    	var time_variable;
     
    	function getXMLObject()  //XML OBJECT
    	{
    		var xmlHttp = false;
    		try {
    			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
    		}
    		catch (e) {
    			try {
    				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
    			}
    			catch (e2) {
    				xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
    			}
    		}
    		if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    			xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
    		}
    		return xmlHttp;  // Mandatory Statement returning the ajax object created
    	}
    
    	var name_='';
     
    	function ajaxFunction(name) {
    		if(xmlhttp) { 
                            $name_ = name;
    			xmlhttp.open("GET","http://localhost/getage.php?name="+$name_ ,true);
    			xmlhttp.onreadystatechange  = handleServerResponse;
    			xmlhttp.send(null);
    		}
    	}
     
    	function handleServerResponse() {
    		if (xmlhttp.readyState == 4) {
    			if(xmlhttp.status == 200) {
    				document.getElementById($name_).value=xmlhttp.responseText; //Update the HTML Form element 
    			}
    			else {
    				alert("Error during AJAX call. Please try again");
    			}
    		}
    	}
    
    	function getage()
    	{
    		ajaxFunction('Tim');
                   
    		ajaxFunction('Kate');
    	}
    PHP:
    I have such code, and when I try to call the function getage(), I see many errors "Error during AJAX call. Please try again", and after clicking OK,OK..., I see only result of function ajaxFunction('Kate'), -> ie Kate's age appears in <input type='text' name='age' id='Kate'>.

    I guess that, the reason is in asynchronous call of ajax - when the first function has not yet received the results, the second is beginning to be run.
    So, how switch these two functions ajaxFunction('Tim') and ajaxFunction('Kate')? Please help.
     
    Last edited: Mar 10, 2010
    Corol, Mar 10, 2010 IP