[英]jQuery .load returning empty div (PHP page)
I have a PHP control panel I'm currently working on.我有一个 PHP 控制面板,目前正在开发中。 I need a div in one of my PHP files to automatically refresh and I came across an jQuery solution which can automatically reload certain parts of a website.
我需要一个 PHP 文件中的 div 来自动刷新,我遇到了一个 jQuery 解决方案,它可以自动重新加载网站的某些部分。 The problem is every time I include the script, when the setInterval runs it just turns the div into a blank element instead of loading the content.
问题是每次我包含脚本时,当 setInterval 运行时,它只会将 div 变成一个空白元素,而不是加载内容。
My site works by having a master PHP called "panel.php" which loads various HTML documents (.php files) using "include".我的网站通过使用名为“panel.php”的主 PHP 来工作,它使用“include”加载各种 HTML 文档(.php 文件)。 One of those HTML documents is called "printers.php".
这些 HTML 文档之一称为“printers.php”。
Inside printers.php I want to reload this div:在 printers.php 中,我想重新加载这个 div:
<div id="tsd"><?php echo time(); ?></div>
So at the top of my code in the head tag of "printers.php" I added this code:因此,在“printers.php”的 head 标签中的代码顶部,我添加了以下代码:
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
setInterval(function () {
$( "#tsd" ).load("panel.php #tsd");
}, 2000);
</script>
Now when the panel.php document loads, the timestamp shows for 2 seconds, then disappears and the div element just goes blank with no data in it.现在,当 panel.php 文档加载时,时间戳显示 2 秒,然后消失,div 元素变为空白,其中没有数据。 I can't figure out why it is reloading a blank div?
我不明白为什么它要重新加载一个空白的 div? Any ideas would help.
任何想法都会有所帮助。
You forgot to add the GET
parameters.您忘记添加
GET
参数。 Change your Interval script from:将您的间隔脚本更改为:
$( "#tsd" ).load("panel.php #tsd");
to到
$('#tsd').load('panel.php?page=printers #tsd');
Upon inspection of your panel.php
code, I noticed this if
statement:在检查你的
panel.php
代码后,我注意到这个if
语句:
if (!isset($_GET['page']))
header('location: panel.php?page=dashboard');
else
include('restricted/sitepages/'.$_GET['page'].'.php');
Thereby, because you were omitting the GET
parameter page
it was loading the Dashboard page instead of the Printers page;因此,因为您省略了
GET
参数page
它正在加载仪表板页面而不是打印机页面; and I'm guessing the Dashboard page does not have a div with the id tsd
.我猜Dashboard页面没有带有 id
tsd
的 div 。 Therefore it was loading nothing because #tsd
does not exist in the loaded response.因此它没有加载任何内容,因为
#tsd
在加载的响应中不存在。
Place the html code in the panel.php and make a recursion there.将 html 代码放在 panel.php 中并在那里进行递归。 So that panel.php looks like this
所以 panel.php 看起来像这样
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready( function(){
refreshClock();
});
function refreshClock(){
$('#tsd').load('printers.php', function(){
setTimeout(refreshClock, 2000);
});
}
</script>
<div id="tsd"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.