Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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:
    Code:
    <div class="status" id="<?php echo $match_id ?>">
                        	<script type="text/javascript"> getStatus('<?php echo $match_id ?>'); </script> 
                        </div>
    The Javascript code:
    Code:
    <script language="javascript" type="text/javascript">
    	xmlhttp = new XMLHttpRequest();
    	function getStatus(id)
    	{	
    		xmlhttp.onreadystatechange = 
    		function()
    		{
    			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);
    		xmlhttp.send();
    	}
    
    </script>
    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
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 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
    •