php+ajax实时刷新简单实例
本文实例讲述了php+ajax实时刷新简单实现方法,分享给大家供大家参考。具体如下:
ajax自动刷新好像是个很常见的问题,之前做一个网页聊天室程序也是卡在了这上面,经过了这段时间的学习,终于做出了一个可以自动刷新网页的代码框架,希望正在迷茫的亲们不要像我一样走了这么多弯路
废话不多说上代码:
html部分:
<html>
<head>
<scripttype="text/javascript">
functionloadXMLDoc()//ajax发送请求并显示
{
varxmlhttp;
if(window.XMLHttpRequest)
{//codeforIE7+,Firefox,Chrome,Opera,Safari
xmlhttp=newXMLHttpRequest();
}
else
{//codeforIE6,IE5
xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","/chat.php",true);
xmlhttp.send();
setTimeout("loadXMLDoc()",1000);//递归调用
}
loadXMLDoc();//先执行一次
</script>
</head>
<body>
<buttontype="button"onclick="loadXMLDoc()">手动刷新</button>
<divid="myDiv"></div>
</body>
</html>
php部分(只是个测试实时刷新的网页)
<?php
/*
1.读取文件
2.推送显示
3.
*/
echofile_get_contents("data.dat");
?>
这样只要修改data.dat就可以实时在网页上显示了。
希望本文所述对大家的php程序设计有所帮助。