Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript PHP
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:
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 22-Aug-12 4:01am
supernorb2.5K
Edited 22-Aug-12 11:07am
v2
Comments
gladiatron at 22-Aug-12 10:38am
   
You can look at using jQuery AJAX which is a much cleaner way of doing AJAX processing.
Ed Nutting at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 841
1 OriginalGriff 245
2 Volynsky Alex 205
3 Kishore Pogaru 140
4 Magic Wonder 129
0 OriginalGriff 5,640
1 Sergey Alexandrovich Kryukov 4,693
2 CPallini 4,540
3 George Jonsson 3,132
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100