Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-12-2007, 05:14 PM   PM User | #1
bilischan88
New Coder

 
Join Date: Nov 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
bilischan88 is an unknown quantity at this point
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?
bilischan88 is offline   Reply With Quote
Old 11-12-2007, 05:26 PM   PM User | #2
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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 05:32 PM..
BarrMan is offline   Reply With Quote
Old 11-12-2007, 06:26 PM   PM User | #3
bilischan88
New Coder

 
Join Date: Nov 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
bilischan88 is an unknown quantity at this point
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.
bilischan88 is offline   Reply With Quote
Old 11-12-2007, 06:55 PM   PM User | #4
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
Try:
Code:
xmlHttp.onreadystatechange=function(){callback(place)};
BarrMan is offline   Reply With Quote
Old 11-12-2007, 07:16 PM   PM User | #5
bilischan88
New Coder

 
Join Date: Nov 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
bilischan88 is an unknown quantity at this point
It didn't work.
bilischan88 is offline   Reply With Quote
Old 11-12-2007, 07:52 PM   PM User | #6
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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?
BarrMan is offline   Reply With Quote
Old 11-12-2007, 10:02 PM   PM User | #7
bilischan88
New Coder

 
Join Date: Nov 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
bilischan88 is an unknown quantity at this point
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 10:15 PM..
bilischan88 is offline   Reply With Quote
Old 11-12-2007, 10:27 PM   PM User | #8
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
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.
BarrMan is offline   Reply With Quote
Users who have thanked BarrMan for this post:
bilischan88 (11-12-2007)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:49 AM.


Advertisement
Log in to turn off these ads.