自动刷新回声 [英] Auto-refresh echo
问题描述
我有一个代码:
<span id="refresh"><?echo $users;?></span>
这里出现注册的用户总数,我想自动刷新它,例如,如果出现100,则当某人注册到我的网站时,我希望将此数字从100自动更新为101,而无需刷新页面.
Here appear total users registered and I want to auto refresh this, for example if appear 100, when somebody has registered to my site, I want to auto update this number from 100 to 101 without refreshing page.
推荐答案
仅在对页面进行预处理时,才将php变量渲染到屏幕上.您将不想使用回声,而是要使用AJAX来获取用户数.
A php varible is only rendered to the screen as the page is being preprocessed. Rather than have the echo there you'll want to use AJAX to fetch the user count.
$.ajax({
type : 'GET',
url : 'user_count.php',
success : function(data){
$('#refresh').html(data);
},
});
以上示例使用的是jQuery的ajax方法.您可以在计时器中调用它,并在user_count.php中回显用户计数.然后,将使用从AJAX请求中收集的计数来设置刷新的内容.
The example above is using jQuery's ajax method. You could call this in a timer and echo out the user count in user_count.php. This would then set the contents of refresh with the count gathered from the AJAX request.
希望这对您有所帮助.
蒂姆
这篇关于自动刷新回声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!