PHP / Ajax results being cached???

I have a website running and I am using an ajax live search to retrieve mysql records. It apears that on some computers the live search is being cached and they do not see the new records that have been added since they logged into the site. If they close their browser and reopen it all is well. Any ideas why this would be happening would be great. I really dont understand how ajax works and this is just killing me… :slight_smile:

LiveSearch.php
[php]
mysql_select_db(“pcon”, $con);
$sql=“SELECT * FROM company WHERE name LIKE '%”.$q."%’";
$result = mysql_query($sql);

?>

<?

while($row = mysql_fetch_array($result))
{
if ($color == “1”) {
$color = “2”;

	?>

	<tr>
		<td width="709" valign="TOP" class="stevenrow"><?php echo $row['name']; ?></td>
		<td width="80" valign="TOP" class="stevenrow"> <a href="companyview.php?cn=<?php echo $row['name']; ?>&cid=<?php echo $row['ID']; ?>">View</a> - <a href="companyedit.php?cn=<?php echo $row['name']; ?>&cid=<?php echo $row['ID']; ?>">Edit</a> </td>
	</tr>
<?php } else { $color = "1"; ?>
	<tr>
		<td width="709" valign="TOP" class="stoddrow"><?php echo $row['name']; ?></td>
		<td width="80" valign="TOP" class="stoddrow"> <a href="companyview.php?cn=<?php echo $row['name']; ?>&cid=<?php echo $row['ID']; ?>">View</a> - <a href="companyedit.php?cn=<?php echo $row['name']; ?>&cid=<?php echo $row['ID']; ?>">Edit</a> </td>
	<?
}

}
mysql_close($con);
?>

[/php]

The Ajax Code:
[php]

[/php]

Company Name  

The common trick to get aroung this problem - append a timestamp to your url, so that each time Ajax is forced to request ‘new’ page from a server. Here is how you can modify your javascript code for this:

var d = new Date(); var ts = d.getTime(); xmlhttp.open("GET","livesearch.php?q="+str+"&ts="+ts,true); xmlhttp.send();

Sponsor our Newsletter | Privacy Policy | Terms of Service