我有一个PHP页面,需要刷新每5秒。在嵌入ajax文件时,我没有发现在Firebug中发生的更新。这里是代码的骨架:刷新PHP页面使用ajax
**notification.php**
<?php
....
....
?>
<html>
<head>
<script src="./refresh.js"></script>
<script type="text/javascript">
refreshContents();
</script>
</head>
<body>
....
<div id="identifier">
<p>Waiting area</p>
</div>
</body>
</html>
**refresh.js**
var seconds = 5;
var content = "identifier";
var url = "notification.php";
function refreshContents()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(f)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(g)
{
alert("Browser not supports Ajax");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState == 4)
{
document.getElementById(content).innerHTML = xmlHttp.responseText;
setTimeout('refreshContents()', seconds*1000);
}
}
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
var seconds = 5;
window.onload = function startrefresh(){
setTimeout('refreshContents()', seconds*1000);
}
因为我需要更新以及每5secs数据库。 – 2011-06-02 19:59:50
在您的PHP代码中触发它们,因为每次页面刷新时都会运行它 – Ryan 2011-06-02 20:00:32
似乎没有工作。这真的很奇怪!是否因为notification.php在开始时创建了一个cookie? – 2011-06-02 20:12:13