【问题标题】:Reload <body> with jQuery and PHP用 jQuery 和 PHP 重新加载 <body>
【发布时间】:2012-11-06 10:03:44
【问题描述】:

我有一个 index.php,每秒钟更新一次用户会话:

[Index.php(之前)]

<script type="text/javascript">
$(function($) {
    var refresh = setInterval(function() {
        $.post('myfolder/reload_info.post.php', {
            id: <?php echo $_SESSION['id']; ?>
        }, function(data){
            clearInterval(this);
        });
    }, 1000);
});
</script>

[reload_info.post.php]

(sql query... // $row is the result)
$_SESSION['name'] = $row->name;
$_SESSION['mail'] = $row->mail;
$_SESSION['status'] = $row->stat;
$_SESSION['cf'] = $row->cf;

如果数据库(MySQL)发生任何变化,通常会收取使用任何会话的站点的所有功能。我想要的是在不重新加载页面的情况下更新 index.php() 的正文。

也就是说,如果我打开 index.php 文件并且在文件和编辑文件之间没有任何内容,我会在此之后放置任何文本,携带文本而无需按 F5 或获取 location.reload()。

有可能吗?谢谢你。 :)

【问题讨论】:

  • 重新加载整个身体有什么意义?为什么不只是你需要改变的部分?我想我没有看到此时不重新加载页面的好处。
  • 在里面我只需要div.container就行了。

标签: php jquery mysql ajax reload


【解决方案1】:

也许你正在寻找类似 joconut 的东西:https://github.com/vdemedes/joconut

【讨论】:

    【解决方案2】:

    你想要这样的东西吗?

    <script type="text/javascript">
    $(function($) {
    var id="<?php echo $_SESSION['id']; ?>";
        var refresh = setInterval(function() {
            $.post('myfolder/reload_info.post.php', {
                id: id;
            }, function(data){
                clearInterval(this);
                $('body .container').html(data);
            });
        }, 1000);
    });
    </script>
    

    在 reload_info.post.php 你应该返回一些东西......

    回声回声 $_SESSION['id'];

    【讨论】:

    • replace() 真的是函数?!
    • 不,所有内容都消失了(仅文本 lorem ipsum)并且没有返回任何内容...
    • 会更新,据我了解,但混合 php 和 js 是个坏主意
    • 嗯,我明白了。嗯,感谢您的帮助!
    • 是的!这正是我想要的。
    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多