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 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    No response(Text) from Ajax pt. 2

    Whenever I run my ajax script it seems to get caught up in the callback function.
    Code:
    function callback(id){ 
    	if (xmlHttp.readyState==4){ 
    		if(xmlHttp.status==200){
    			document.getElementById(id).innerHTML=xmlHttp.responseText;
    		}else{
    			document.getElementById(id).innerHTML="Cannot find page: " + xmlHttp.status;
    		}
    	}else{
    		document.getElementById(id).innerHTML="Waiting for server...";
    	}
    }
    It's supposed to display the responseText (obviously) but it always displays the "waiting for server" text and never moves on. When checking it in firebug it says it executed correctly and it even shows me the responseText that firebug says should've been displayed. However it doesn't display it in the browser. What's up with that?

  • #2
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    What's your other AJAX code? The one that is calling the function.
    What's the element that is supposed to display the content?
    Last edited by BarrMan; 11-12-2007 at 06:32 PM.

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    sorry, here's the function that's called by the event and calls the callback function.
    Code:
    function eventHorizon(place, which){
    	var dropdown 	= document.getElementById(which+'_country');
    	var choice		= dropdown.selectedIndex;
    	var choice_text	= dropdown.options[choice].text;
    	var num;
    		switch(choice_text){
    			default:
    				num = 0;
    				break
    			case 'United States of America':
    				num = 1;
    				break
    			case 'Canada':
    				num = 3;
    				break
    			case 'United Kingdom':
    				num = 2;
    				break
    		}
    	if(num != 0){
    		document.getElementById(place).innerHTML="Starting...";
    		xmlHttp=createXMLHttpRequest();
    			var url="u_info_supp.php?id="+num;
    		xmlHttp.onreadystatechange=callback(place)
    		xmlHttp.open("GET",url,true);
    		xmlHttp.send(null);
    	}else{
    		document.getElementById(place).innerHTML='';	
    	}
    }
    here's the html.
    Code:
    <p id="place2"></p>
    <p>Country: <select name="c_country" id="c_country" onchange="eventHorizon('place2', 'c')">
    					<?php
    					for($x=0; $x < count($countries[0]); $x++){
    						if(($countries[0][$x] == 'United States of America') || ($c_country == $countries[0][$x])){
    							echo "<option value=\"".$countries[0][$x]."\" selected=\"selected\">".$countries[0][$x]."</option>";
    						}else{
    							echo "<option value=\"".$countries[0][$x]."\">".$countries[0][$x]."</option>";
    						}
    					}
    					?></select></p>
    id "place2" is where the response text goes.

  • #4
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Try:
    Code:
    xmlHttp.onreadystatechange=function(){callback(place)};

  • #5
    New Coder
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It didn't work.

  • #6
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by bilischan88 View Post
    It didn't work.
    And how's the content of the page u_info_supp.php viewed?
    Have you checked the url?

  • #7
    New Coder
    Join Date
    Nov 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Actually your trick did work. The js script was just cached. It all works now. Thanks.
    Just out of curiosity what's the difference b/w your way and mine.
    Last edited by bilischan88; 11-12-2007 at 11:15 PM.

  • #8
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    when you do var s=my_function(); it calls the function immediately when you write it but if you do var s=my_function; or var s=function(){my_function();}; it will call it only when you use the variable.

    On this example it wouldn't quite work because the s variable is not a class or anything that needs to be called in order to initiate it but take for instance an onclick event, it would work there the same.

  • Users who have thanked BarrMan for this post:

    bilischan88 (11-12-2007)


  •  

    Posting Permissions

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