Home  >  Article  >  Backend Development  >  php+ajax实时刷新简单实例_php技巧

php+ajax实时刷新简单实例_php技巧

WBOY
WBOYOriginal
2016-05-16 20:22:44909browse

本文实例讲述了php+ajax实时刷新简单实现方法,分享给大家供大家参考。具体如下:

ajax自动刷新好像是个很常见的问题,之前做一个网页聊天室程序也是卡在了这上面,经过了这段时间的学习,终于做出了一个可以自动刷新网页的代码框架,希望正在迷茫的亲们不要像我一样走了这么多弯路
废话不多说 上代码:

html部分:

<html> 
<head> 
<script type="text/javascript"> 
function loadXMLDoc()//ajax发送请求并显示 
{ 
var xmlhttp; 
if (window.XMLHttpRequest) 
 {// code for IE7+, Firefox, Chrome, Opera, Safari 
 xmlhttp=new XMLHttpRequest(); 
 } 
else 
 {// code for IE6, IE5 
 xmlhttp=new ActiveXObject("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> 
<button type="button" onclick="loadXMLDoc()">手动刷新</button> 
<div id="myDiv"></div> 
</body> 
</html> 

php部分(只是个测试实时刷新的网页)

<&#63;php 
/* 
1.读取文件 
2.推送显示 
3. 
*/ 
echo file_get_contents("data.dat"); 
 
&#63;> 

这样只要修改data.dat就可以实时在网页上显示了。

希望本文所述对大家的php程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn