tags:

views:

42

answers:

4

I'm experimenting on this code that I got from the net (I'm trying to make a simple chat but i do the message insertion manually in mysql database). It refreshes the page every 3 seconds and displays the new message in the page fetched from database. It works well in chrome and firefox but it doesn't in IE. What I have observed in IE is it will only display the new message every time I clear the cache or delete the cookies in the browser. Can anyone help me to devise a solution for this please...

Please see code:

    <html>
    <head>
    <script type="text/javascript">
    function showResult(str) {
    document.getElementById("livesearch").innerHTML="";
     if (str.length==0) { 
      document.getElementById("livesearch").innerHTML="";
      document.getElementById("livesearch").style.border="0px";
      return;
     }

     if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
     }
     else {
     // code for IE6, IE5
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     var msxmlhttp = new Array(
        'Msxml2.XMLHTTP.5.0',
        'Msxml2.XMLHTTP.4.0',
        'Msxml2.XMLHTTP.3.0',
        'Msxml2.XMLHTTP',
        'Msxml2.xmlHTTP
        'Microsoft.XMLHTTP');
     for (var i = 0; i <= msxmlhttp.length; i++) {
      try {
      xmlhttp = new ActiveXObject(msxmlhttp[i]);
      } 
      catch (e) {
      xmlhttp = null;
      }
     }
     }

     xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
       document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
       document.getElementById("livesearch").style.border="1px solid #A5ACB2";
       setTimeout("showResult('a')", 3000);
      }
     }
     xmlhttp.open("GET","http://localhost/review/login/5/try.php",true);
     xmlhttp.send();
    }
    </script>
    </head>
    <body onload="">

<script>
 setTimeout("showResult('a')", 3000);</script>
<form>

<div id="livesearch"></div>
</form>

</body>
</html>

this is the code for try.php

<?php
require_once('config.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
 if(!$link) 
 {
  die('Failed to connect to server: ' . mysql_error());
 }

 $db = mysql_select_db(DB_DATABASE);
 if(!$db) 
 {
  die("Unable to select database");
 }

//Create query
 $qry="SELECT * FROM message where message like '%".$_GET['a']."%'";
 $result=mysql_query($qry);

 if(mysql_num_rows($result) > 0)
 {
  while($row = mysql_fetch_assoc($result))
  {
   echo $row['message']."<br>";
   //echo "<br> ".$member['message']; 
  }
 }
?>

Please help...thanks...

A: 

It looks like a caching issue. You should try adding a random number to your URL. That will force IE to reload the page.

xmlhttp.open("GET","http://localhost/review/login/5/try.php?r=" + Math.random(),true);
svanryckeghem
A: 

I know that won't help you right now, at the first time, but I highly recommend you to use a JS library like jQuery. It makes ajax calls a lot simpler, and cross-browser. It will make your code clearer, and you will be able to focus on your logic instead of low level browser issues.

PJP
how sir.......?
yonan2236
jQuery is a very helpful JavaScript library. An ajax GET call is made simply like this:$.get(URL, function() { //Your code to handle server response});Maybe your browser compatibility issue can be resolved by using jQuery (OK, you can also fix it by yourself, but really... jQuery is worth it).
PJP
+1  A: 

Your best bet is to add the following headers to the top of your page that is to return the content you wish not to be cached

header('Last-Modified: Mon, 01 Jan 1970 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');

This will allow you to control the cache on the server side rather then adding the "random" number hack at the end of the request.

Tanerax
ok...so I just have to copy your provided code to my first page?
yonan2236
try adding it in your try.php, just after the require_once function.
Tanerax
ok thank you very much....
yonan2236
A: 

If you want to do AJAX stuff and have it work in multiple browsers I recommend taking a look at jQuery:

jQuery Home Page

It is reasonably easy to get started and it will make you much more productive.

In particular, take a look at jQuery.ajax():

jQuery.ajax()

You will probably initially experience the same problem with IE when using jQuery, but I think you can fix it by setting the cache parameter to false (as described on the page I link to above).

Rune