Click here to Skip to main content
13,044,584 members (62,296 online)
Rate this:
Please Sign up or sign in to 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:
function getMsg() {
  if(aCyclePassed) {
   aCyclePassed = false;
   var receiver = (XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttpRequest") ;"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";
 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
Updated 22-Aug-12 11:07am
gladiatron 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.

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

Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2017
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