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>
php part (just a web page to test real-time refresh)
<?php /* 1.读取文件 2.推送显示 3. */ echo file_get_contents("data.dat"); ?>
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.