jQuery的/ AJAX加载新的内容时可用 [英] jquery/ajax load new content when available

查看:143
本文介绍了jQuery的/ AJAX加载新的内容时可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个相当模糊的问题 - 我们对此深感抱歉。如果我有一个论坛,在线留言板或类似的,用户输入并提交数据什么的,PHP和MySQL的运行,什么是有新提交的内容会自动显示当它被提交的所有用户在页面上的最好的方法是什么?

I know this is quite a vague question -- sorry about that. If I have a forum, shoutbox or something similar where users enter and submit data, running on PHP and MySQL, what is the best way to have the newly submitted content automatically display on the page for all users when it is submitted?

就像一个活的新闻源,如果你喜欢...的影响排序的作品在这里计算器,当你回答一个问题,你被告知,当新的答案被提交。我想测试新提交的内容,然后自动显示出来。

Much like a live newsfeed, if you like... The effect sort-of works here at stackoverflow, when you are answering a question you are told when new answer is submitted. I want to test for newly submitted content and then display it automatically.

有什么建议?非常感谢:)

Any suggestions? Many thanks :)

推荐答案

显然,这弹簧想到的第一件事情是一个轻量级的AJAX请求每x秒,以检查是否有新的内容。

Obviously the first thing that springs to mind is a lightweight AJAX request every x seconds to check for new content.

沿 http://buntin.org/2008/的东西线9月23日/ jQuery的轮询-插件/

编辑: 这是未经测试。

AJAX调用 http://example.com?lastCheck=1273244156

PHP: 
<?
    if(isset($_GET['lastCheck'])){
        $ts = mysql_real_escape_string($_GET['lastCheck']);

        $result = mysql_query("SELECT * FROM `table` WHERE `timestamp` >= {$ts}");
        $rows = mysql_fetch_array($result, MYSQL_ASSOC);

        if($rows){
            header('Cache-Control: no-cache, must-revalidate');
            header('Content-type: application/json');
            echo json_encode($rows);
        }
    }
?>

然后使用jQuery来检查,如果Ajax响应是有效的JSON,如果是建立你的项目,并把它们添加到您的容器。

Then use jQuery to check if the AJAX response is valid JSON, if so build your items and append them to your container.

这篇关于jQuery的/ AJAX加载新的内容时可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆