Click here to Skip to main content
15,885,074 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I'm encountering a very strange problem with calling send() of a XMLHttpRequest object periodically. The send() will submit to a php page (silently), this page will fetch some data in the database, echo the data (in plain text format) and then delete the data. The client side will receive the echoed plain text and should receive it once time, but when the send() is called the second time (and later times), the deleted text is still received, unless I reload the page.

I think there is something wrong with how AJAX works. Here is a snippet of my code:
JavaScript
function getMsg() {
  if(aCyclePassed) {
   aCyclePassed = false;
   var receiver = (XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttpRequest") ;
  receiver.open("GET", "http://localhost/chatting.php?u=currentUser", true) ;
  receiver.onreadystatechange = function() {
  if(receiver.readyState ==4 && receiver.status ==200) {
  var msg = receiver.responseText ;
  aCyclePassed = true;
   if(msg != "") chatBox.value += msg + "\n";
   }
 };
 receiver.send();
}
 setTimeout("getMsg()", 1000);
}

The chatting.php has nothing special than getting the variable u from client, select some text (message) from database, echo it and then delete it from database to make client fetch and display it only one time, but as I said, it seems not to be deleted (in fact it was) until I reload the client page.

Could you help me out please? This is the first online chatting application using PHP of mine, thank you very much!
Posted
Updated 22-Aug-12 11:07am
v2
Comments
I.explore.code 22-Aug-12 10:38am    
You can look at using jQuery AJAX which is a much cleaner way of doing AJAX processing.
Ed Nutting 22-Aug-12 14:46pm    
This looks like a caching issue (i.e. your AJAX request's response is being cached by the browser). This is a common problem. jQuery (as has been mentioned) is a very good way of cleanly solving all these sorts of issues.

Ed
supernorb 23-Aug-12 1:29am    
Well, I think so, but I don't know how to solve it using just javascript and PHP, unfortunately I have never learned jQuery although I've heard of it for years.
Is there a solution without using jQuery? I have read somewhere on the internet that they set some header info to invalidate the caching on client side, it may be the solution for me.
Thank you!

1 solution

The above given ajax code may work in some of the browsers like mozilla,opera but they not mean IE,so you can better use JQuery to use them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900