dousou1878 2015-02-02 11:08
浏览 58
已采纳

自动刷新PHP脚本后端[关闭]

I'm looking for a script that will automatically update a page but not reload it. I have one example, these divs are automatic reloading: http://prntscr.com/603k2n

It's not my page of course, here is link: http://suna.e-sim.org/battle.html?id=13317 (you will need to register).

I tried with jQuery but it won't reload PHP, just HTML. I need to reload MySQL and PHP - is it even possible? i see this guys are made it.

  • 写回答

2条回答 默认 最新

  • douzhaolu4780 2015-02-02 11:50
    关注

    To refresh a page without reloading, you have to load the contents of the page through ajax. You could do this as follows:

    index.php

    <html>
    <head>
    <script type="text/javascript">
    $(document).on('ready', function(){
        setInterval(function() {
            $.ajax({
                type: "GET",
                url: "ajax_refresh.php",
                success: function(result) {
                    $('body').html($result);
                }
            });
        }, 3000);
    });
    </script>
    </head>
    <body>
        <div class="header">
        Header of website here
        </div>
        <div class="content">
        Content here
        </div>
        <div class="footer">
        Footer here
        </div>
    </body>
    </html>
    

    ajax_refresh.php

        <div class="header">
        Header of website here
        </div>
        <div class="content">
        Content here
        </div>
        <div class="footer">
        Footer here
        </div>
    

    In the above example the 'url' parameter should be a PHP file that only returns the body of the page you want to refresh. For this example to work, you should include jQuery.

    Good luck

    Edit

    To explain this a little more. You will need a second file that looks exactly te same as your index file. Except that in this second file you do not have html, head or body tags. The content of the second file will be loaded into the first file without refreshing. This is the concept of AJAX.

    For further reading: - Introduction to AJAX - W3Schools

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记