我有一个HTML页面,其中的div包含来自mysql查询的php变量的值。我希望div自动刷新,以便查看来自SQL查询的任何新数据。我找了一周的代码,但一无所获。。div代码如下:
<div id="last10">
<table width="838" cellpadding="1" cellspacing="1" class="categorie1">
<tr style='font: bold 12px verdana;'>
<td width="149" align="center">Browser</td>
<td width="99" align="center">OS</td>
<td width="248" align="center">Date</td>
<td width="103" align="center">IP</td>
<td width="108" align="center">Country</td>
<td width="110" align="center">Referrer</td>
</tr>
{$recent_visits}
</table>
</div>
发布于 2011-08-03 01:42:33
PHP是服务器端语言,HTML和JavaScript是客户端语言。因此,您需要使用AJAX通过PHP更新HTML/JavaScript中的一些数据
发布于 2011-08-03 01:43:09
如果您希望数据自动刷新,则必须使用一些ajax魔术。真的需要更多的信息。什么是$recent_visits?
发布于 2018-06-29 05:01:31
前段时间,我自己的项目也需要类似的东西。我知道我来晚了,但我希望这能帮助其他正在寻找同样解决方案的人。
首先,创建一个文件。让我们称它为data.php
。这个文件应该包含从数据库中检索数据的所有代码,这是您将任何相关数据放入变量的位置。这是我自己项目中的一些示例代码:
<?php
$cpu_load = sys_getloadavg();
$total_storage = disk_total_space("/");
$free_storage = disk_free_space("/");
$public_ip = file_get_contents("public_ip.txt");
?>
这段代码所做的一切就是设置变量,这是唯一会定期刷新的位。您还需要在此处添加DIV的内容(虽然没有DIV标记)。这部分应该看起来像这样(显然替换了我的示例变量):
<table width="838" cellpadding="1" cellspacing="1" class="categorie1">
<tr style='font: bold 12px verdana;'>
<td width="149" align="center">Average CPU Load: <?php echo $cpu_load; ?></td>
<td width="99" align="center">Total Storage: <?php echo $total_storage; ?></td>
<td width="248" align="center">Free Storage: <?php echo $free_storage; ?></td>
<td width="103" align="center">Public IP: <?php echo $public_ip; ?></td>
</tr>
</table>
接下来,在您的主页中,您可以使用如下内容:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$("#refresh").click(function() {
$("#last10").load("data.php");
return false;
});
});
function refresh() {
$("#refresh").click();
}
setInterval(refresh, 100);
</script>
<a href="#" id="refresh" hidden="">Refresh</a>
<div id="last10"></div>
这只是创建了一个隐藏的链接,然后“点击”它来刷新div。
我认为这应该能回答你的问题。如果您需要任何澄清,请在评论中提出。希望这对一些人有帮助:)
https://stackoverflow.com/questions/6916167
复制相似问题