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 07-06-2007, 10:11 PM   PM User | #1
TriKri
New Coder

 
Join Date: Jul 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
TriKri is an unknown quantity at this point
AJAX function only works once!

Hello! I have written this test code:

Code:
<html>
<body>
                          
<script type="text/javascript">

function ajaxFunction() {
  var xmlHttp;
  try { //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e) {
    try { //Internet Explorer
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      alert("Your browser does not support AJAX!");
      return false;
    }
  }

  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState==4) {
      document.myForm.time.value = xmlHttp.responseText;
      xmlHttp.responseText = "";
    }
  }
  xmlHttp.open("get","time.php",true);
  xmlHttp.send(null);
}

</script>

<form name="myForm">
  Name: <input type="text" onkeyup="ajaxFunction();" name="username" />
  Time: <input type="text" name="time" />
</form>

</body>
</html>
And in time.php:
Code:
<?php
  echo date("H:i:s");
?>
And it works perfectly fine, the first time. But the second time and the rest of the times (after clearing the textboxes), the same time as before will continue to pop up, each time I release a key. But it should be the current time, not the same time as before! I would really appreciate some help here. What in my code is wrong? Or is it IE 7 that troubles me?
TriKri is offline   Reply With Quote
Old 07-06-2007, 11:50 PM   PM User | #2
carlitos_way
New Coder

 
Join Date: Mar 2007
Location: italy
Posts: 94
Thanks: 1
Thanked 1 Time in 1 Post
carlitos_way is an unknown quantity at this point
I think (but if i'm thinking wrong, correct me) that IE caches the result, so it's always the same.

this is a classical problem, that can be avoided by passing a random useless parameter, or a datetime, so that IE thinks that you're requesting something new, and it avoids to cache the result.
carlitos_way is offline   Reply With Quote
Old 07-07-2007, 01:12 AM   PM User | #3
TriKri
New Coder

 
Join Date: Jul 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
TriKri is an unknown quantity at this point
Quote:
Originally Posted by carlitos_way View Post
I think (but if i'm thinking wrong, correct me) that IE caches the result, so it's always the same.

this is a classical problem, that can be avoided by passing a random useless parameter, or a datetime, so that IE thinks that you're requesting something new, and it avoids to cache the result.
Wow, thanks! That worked! I just added a junk-counter and added it to the the adress as a GET-variable. Do you know of some way to tell IE to reload the page anyway or maybe to clear that specific history so it will be reloaded?
TriKri is offline   Reply With Quote
Old 07-07-2007, 11:42 PM   PM User | #4
carlitos_way
New Coder

 
Join Date: Mar 2007
Location: italy
Posts: 94
Thanks: 1
Thanked 1 Time in 1 Post
carlitos_way is an unknown quantity at this point
When I use Firefox, I press ctrl+f5 to reload all the page without using the version cached. I think you can try the same on IE

this can be a solution for you, but not for the people who'll see your end application.. but i don't know any other answer, sorry
carlitos_way is offline   Reply With Quote
Old 07-09-2007, 04:16 PM   PM User | #5
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I've got the exact same problem in IE, except adding a random number to the url isnt helping, any ideas?

Code:
var xmlHttp
var field

function suggest_weight()
{
	frame = document.getElementById('users_frame_size')[document.getElementById('users_frame_size').selectedIndex].value;
	height_feet = document.getElementById('users_height_feet')[document.getElementById('users_height_feet').selectedIndex].value;
	height_inches = document.getElementById('users_height_inches')[document.getElementById('users_height_inches').selectedIndex].value;
	url = '/suggest_weight.php?frame='+frame+'&height_feet='+height_feet+'&height_inches='+height_inches;
	changeField(url, 'users_goal_weight');
	
}
function changeField(url,fieldName)
{
 	field = fieldName;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url+'&rand='+Math.floor(Math.random()*100),true);
	xmlHttp.send(null);
} 

function stateChanged() 
{ 
 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById(field).value=xmlHttp.responseText;
	}
}

onload = function()
{
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }
	return xmlHttp;
}
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 07-09-2007, 04:37 PM   PM User | #6
TriKri
New Coder

 
Join Date: Jul 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
TriKri is an unknown quantity at this point
Quote:
Originally Posted by NancyJ View Post
I've got the exact same problem in IE, except adding a random number to the url isnt helping, any ideas?
How random is the random number? (I haven't used that random generator before, I believe, if it isn't the same as in the C/C++ math library) could it maybe have happened that the rendom generator used the same random seed all the times?

Just taking a shot, try adding a counter instead as I did, there could be a chans that helps.
TriKri is offline   Reply With Quote
Old 07-09-2007, 06:23 PM   PM User | #7
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I tried a counter and still no luck. And yes I checked that the counter was being incremented
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 07-09-2007, 06:57 PM   PM User | #8
TriKri
New Coder

 
Join Date: Jul 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
TriKri is an unknown quantity at this point
Quote:
Originally Posted by NancyJ View Post
I tried a counter and still no luck. And yes I checked that the counter was being incremented
You have? I was just gonna ask you that!

Last edited by TriKri; 07-09-2007 at 07:04 PM..
TriKri is offline   Reply With Quote
Old 07-10-2007, 01:19 AM   PM User | #9
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Using POST would clear it up since GET is supposed to cache.

you could also do
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 07-10-2007, 04:56 PM   PM User | #10
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
tried both the header and the post method - still no luck, it still only works the first time in IE
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 07-10-2007, 05:09 PM   PM User | #11
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Are you reusing the Object?

The correct order with IE is

open()
onreadystatechange
send()

If you have onreadystatechange before the open, IE will not work.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 07-10-2007, 09:51 PM   PM User | #12
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You're only setting the xmlHttp variables once. Try creating it everytime changeField function is called.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 07-10-2007, 10:00 PM   PM User | #13
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
alien51 had it - it was the ordering of open onreadystatechange and send, that'll teach me to grab code from w3schools rather than walking up to my pc and getting my own
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 07-12-2007, 04:52 AM   PM User | #14
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
A bunch of people complain about that issue with the W3Schools code. A bunch of pople have sent them emails telling them to change it, but they never did.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 12-12-2007, 03:39 PM   PM User | #15
SquidScareMe
New to the CF scene

 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
SquidScareMe is an unknown quantity at this point
Smile Thanks Alien51

Thanks Alien51. You just saved me.
SquidScareMe is offline   Reply With Quote
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 08:35 AM.


Advertisement
Log in to turn off these ads.