Simple example of php+ajax real-time refresh, phpajax real-time example_PHP tutorial

WBOY
Release: 2016-07-13 10:06:05
Original
810 people have browsed it

php+ajax real-time refresh simple instance, phpajax real-time instance

The example in this article describes the simple implementation method of php+ajax real-time refresh, and shares it with everyone for your reference. The details are as follows:

Ajax automatic refresh seems to be a very common problem. I was stuck on this when I was making a web chat room program. After this period of study, I finally made a code framework that can automatically refresh the web page. I hope it will be done soon. Confused friends, don’t take so many detours like me
Without further ado, here’s the code:

html part:

<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> 
Copy after login

php part (just a web page to test real-time refresh)

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

In this way, as long as data.dat is modified, it can be displayed on the web page in real time.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/960719.htmlTechArticlephp+ajax real-time refresh simple example, phpajax real-time example This article describes the simple implementation method of php+ajax real-time refresh, Share it with everyone for your reference. The details are as follows: ajax automatic refresh...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!