dousou1878 2015-02-02 11:08 采纳率: 0%
浏览 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条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境