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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?