Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Thanked 0 Times in 0 Posts

    Question Calling multiple Ajax Functions overides response

    Hello, I am new to ajax but need to solve a what seems to me a tricky situation.

    I receive some live soccer matches data through some xml files that are put in a server. One of the attributes of the xml items is "status" which when a match is currently playing displays the minutes. I build a grid with div-s while reading the xml, and in some particular div I call the function that triggers the ajax, which should return the result to the div which called it.

    The php code:
    <div class="status" id="<?php echo $match_id ?>">
                        	<script type="text/javascript"> getStatus('<?php echo $match_id ?>'); </script> 
    The Javascript code:
    <script language="javascript" type="text/javascript">
    	xmlhttp = new XMLHttpRequest();
    	function getStatus(id)
    		xmlhttp.onreadystatechange = 
    			if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
    			{ document.getElementById(id).innerHTML = xmlhttp.responseText;
    			} else 
    			{ document.getElementById(id).innerHTML = "...";
            xmlhttp.open("GET", "functions/returnStatus.php?match_id="+id, true);
    For some reason (that i don't know) The ajax returns "..." to all divs except the last one, where it returns the proper result.
    Please help, at least with any idea where to start searching for solutions.
    Thanks in advance!

  2. #2
    Senior Coder
    Join Date
    Dec 2010
    Thanked 570 Times in 563 Posts
    You are using the same XMLHttpRequest object for all the elements. So (except for the last element) there will be only one call for onreadystatechange for each element. Only the last element will reach the readyState==4


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts