刷新AJAX数据每隔X分钟/秒,jQuery的 [英] Reload AJAX data every X minutes/seconds, jQuery
问题描述
我编一个CMS,有一个日志谁最近登录到系统中。 presently,该数据被送入通过Ajax一个jQuery UI选项卡。我希望把这个信息到侧边栏的主网页,并通过AJAX加载它每隔30秒(或一定的时间规定的期限)。
I programmed a CMS that has a log of who has recently logged into the system. Presently, this data is fed into a jQuery UI tab via Ajax. I would like to put this information into a sidebar on the main page and load it via AJAX every 30 seconds (or some set period of time).
我怎么会去这样做呢?请问PHP反应必须是JSON codeD?我是相当新的AJAX和JSON数据。
How would I go about doing this? Does the PHP response need to be JSON coded? I am fairly new to AJAX and JSON data.
下面是PHP我目前使用拉从用户表的详细信息 -
Here is the PHP I am currently using to pull details from the users table-
<?php
$loginLog = $db->query("SELECT name_f, name_l, DATE_FORMAT(lastLogin, '%a, %b %D, %Y %h:%i %p') AS lastLogin FROM user_control ORDER BY lastLogin ASC LIMIT 10");
while ($recentLogin = $loginLog->fetch()) {
echo $recentLogin['name_f'] . " " . $recentLogin['name_l'] . " - " . $recentLogin['lastLogin'];
}
?>
谢谢! 更新
好了,这是我迄今为止..部分我卡上是如何通过JSON循环,将其输入框。它工作正常,只要我使用的只是一个结果,并确保它不是在[]的。我只是学习Ajax和JSON,由于某种原因,它不来我太容易了。
Okay, this is what I have so far.. the part I'm stuck on is how to loop through JSON and enter it into the box. It works fine as long as I use just one result and assure it is not in [ ]'s. I'm just learning Ajax and JSON, for some reason it isn't coming to me too easily.
使用Javascript -
Javascript -
$(document).ready(function(){
function refreshUsers(){
$.getJSON('json.php', function(data) {
for (var i = 0; i < data.length; i++) {
$("#loadHere").html('<p>' + data.name + ' ' + data.lastLogin + '</p>');
}
});
}
var refreshInterval = setInterval(refreshUsers, 30 * 1000);
refreshUsers();
});
什么我的PHP脚本输出 -
What my PHP script outputs -
[{"name":"Joe Smith","lastLogin":"Fri, May 21st, 2010 08:07 AM"},{"name":"Jane Doe","lastLogin":"Fri, May 21st, 2010 07:07 AM"}]
谢谢!
推荐答案
PHP端,使用的 json_en code()。
客户端,使用 $ .getJSON():
function refreshUsers(){
$.getJSON(url, postData, function (data, textStatus){
// Do something with the data
});
}
// Keep interval in a variable in case you want to cancel it later.
var refreshInterval = setInterval(refreshUsers, 30 * 1000);
通过这两个,你应该有很多开始使用。不仅如此,你会要求我们为你工作:)
With these 2, you should have a lot to get started with. More than this, you'd be asking us to work for you :)
这篇关于刷新AJAX数据每隔X分钟/秒,jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!