刷新PHP页面用ajax

编程入门 行业动态 更新时间:2024-10-10 23:17:44
本文介绍了刷新PHP页面用ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个PHP页面被刷新,每5秒。在嵌入AJAX的文件,我不觉得发生在Firebug的更新。这里是code骨架:

** notification.php ** < PHP        ....        .... ?> < HTML>      < HEAD>      <脚本SRC =./ refresh.js>< / SCRIPT>      <脚本类型=文/ JavaScript的>          refreshContents();      < / SCRIPT>      < /头>      <身体GT;             ....             < D​​IV ID =标识符>                < P>等候区和LT; / P>             < / DIV>       < /身体GT; < / HTML> ** refresh.js ** VAR秒= 5; VAR内容=标识; VAR URL =notification.php; 功能refreshContents() {    VAR XMLHTTP;    尝试    {       XMLHTTP =新XMLHtt prequest();    }    赶上(E)    {       尝试       {          XMLHTTP =新的ActiveXObject(MSXML2.XMLHTTP);       }       赶上(六)       {          尝试          {             XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);          }          赶上(G)          {             警报(浏览器不支持Ajax的);             返回false;          }       }    }    xmlHttp.onreadystatechange =功能()    {       如果(xmlHttp.readyState == 4)       {          的document.getElementById(内容).innerHTML = xmlHttp.responseText;          的setTimeout('refreshContents()',秒* 1000);       }    }    xmlHttp.open(GET,URL,真正的);    xmlHttp.send(空); } VAR秒= 5; 在window.onload =功能startrefresh(){    的setTimeout('refreshContents()',秒* 1000); }

解决方案

虽然它可能不是理想的解决方案,jQuery有实施的pretty的简单方法正是这一点:

$(文件)。就绪(函数(){   函数重载(){     $(#内容)的负载(notification.php)。   }   的setTimeout(重装,秒* 1000) }

我不知道,这将很好地工作,没有一小会儿做它,但它是一个更优雅的解决方案,我相信。

I have a php page that needs to be refreshed every 5 secs. On embedding the ajax file, I don't find the updates taking place in Firebug. Here is the skeleton of the code:

**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); }

解决方案

Though it may not be the ideal solution, jQuery has a pretty simple way of implementing exactly this:

$(document).ready(function () { function reload() { $("#content").load("notification.php"); } setTimeOut(reload, seconds*1000) }

I'm not sure that will work perfectly, haven't done it in a little while, but its a much more elegant solution I do believe.

更多推荐

刷新PHP页面用ajax

本文发布于:2023-10-10 17:47:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   PHP   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!