自动刷新html页面中的php变量
我有一个 HTML 页面,其中的 div 包含 php 变量的值,该值来自 mysql 查询。我希望 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>
I have an HTML page with a div containing the value of a php variable, which is from a mysql query. I want the div to auto-refresh in order to see any new data from an SQL query. I searched a week for the right code, but nothing... . The div code is the following:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PHP 是服务器端语言,HTML 和 JavaScript 是客户端语言。因此,您需要使用 AJAX 使用 PHP 来更新 HTML/JavaScript 中的一些数据
PHP is a server-side language, HTML and JavaScript are client-side. So, you need to use AJAX to update some data in HTML/JavaScript using PHP
如果您想阻止整个页面刷新,则需要使用 AJAX。基本步骤:
get_recent_visits_count.php
或current_page.php?get_recent_visits
。get
)并检索值setInterval
< /a> (或类似)并定期刷新。如果您想将代码保留在同一页面上,请尝试这样的操作(添加到页面顶部):
然后,在您的 HTML 页面中尝试如下操作:
1st,放置一个要使用新值填充的元素在页面上:
然后,在页面的
部分中,您需要包含 jQuery 和 ajax 代码:
以上只是在页面上显示单个数字的简单方法,但应该让您入门。 PHP 提供了使用
json_encode
转储 JSON 对象的功能 可以与 jQuery 的.getJSON
配对返回更复杂的对象。You're going to need to use AJAX if you want to resist a whole page refresh. Basic steps:
get_recent_visits_count.php
orcurrent_page.php?get_recent_visits
.get
for simplicity) and retreve the valuesetInterval
(or alike) and have it refresh periodically.If you want to keep the code on the same page try something like this (added to the top of the page):
Then, in your HTML page try something like the following:
1st, place an element you want to populate with the new value on the page:
Then, in the
<head>
section of your page you're going to need to include jQuery and the ajax code:The above is just a simple way to get a single number to display on the page, but should get your feet wet. PHP offers the ability to dump JSON objects using
json_encode
which can be paired with jQuery's.getJSON
to return more complex objects.如果您希望数据自动刷新,则必须使用一些 ajax 魔法。确实需要更多信息。 $recent_visits 是什么?
You'll have to use some use some ajax magic if you want the data to refresh automatically. More info is needed really. What's $recent_visits?
不久前我自己的项目需要类似的东西。我知道我“有点”晚了,但我希望这可以帮助其他人寻找相同的解决方案。
首先,制作一个文件。我们将其命名为
data.php
。该文件应包含从数据库检索数据的所有代码,并且您可以在此处将任何相关数据放入变量中。这是我自己项目中的一些示例代码:这些代码所做的只是设置变量,这是唯一会定期刷新的位。您还需要在此处添加 DIV 的内容(尽管没有 DIV 标签)。这部分应该看起来像这样(显然替换了我的示例变量):
接下来,在您的主页中,您可以使用类似这样的内容:
这只是创建一个隐藏链接并“单击”它来刷新 div。
我想这应该可以回答你的问题。如果您需要任何说明,请在评论中询问。希望这对一些人有帮助:)
I needed something similar a while back for my own project. I know I'm a "little" late but I'm hoping this could help other people looking for the same solution.
First, make a file. Let's call it
data.php
. This file should contain all your code to retrieve data from your database and this is where you put any relevant data into variables. This is some example code from my own project:All this code does is it sets the variables and this is the only bit that will periodically be refreshed. You also need to add the contents of the DIV here (without the DIV tags though). This part should look something like this (obviously replacing my example variables):
Next, in your main page you could use something like this:
This simply creates a hidden link and "clicks" it to refresh the div.
I think this should answer your question. Ask in comments if you need any clarification. Hope this helps some people :)