dongxingdu9594
dongxingdu9594
2013-07-02 10:18

在处理php时显示加载消息,然后重定向到另一个页面

已采纳

I am trying to write a page in php which shows a loading message while it does some processing and then auto redirects to another page

<?php
    //show a loading message - this is the bit I need help with
    // do some processing - don't need help with this bit
    header("Location: http://www.mysite.com/mynextpage.php");
    exit;
?>

I can't use echo or javascript otherwise I get a "Cannot modify header information - headers already sent" error when the page executes.

Any clues?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongzhun1857 dongzhun1857 8年前

    Using java script to redirect after the processing has finished seems to be the way to go.

    I'm using

    <script type="text/javascript">
        window.location="http://www.mysite.com/mynextpage.php";
    </script>
    

    at the end of the page and it's working

    点赞 评论 复制链接分享
  • dongzai0020 dongzai0020 8年前

    First of all, you mustn't use any header change after outputing some data, that is why you get the error above. Another way, use header redirections by refreshing page on next page:

    <?php
        header('Refresh: 5; url=http://www.mysite.com/mynextpage.php' ); 
        echo 'Wait 5 sec then redirected';
    

    Note:

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

    点赞 评论 复制链接分享
  • dpbyr64224 dpbyr64224 8年前

    Best way is to use ajax requests. Via javascript you should show the loading element, perform the request, on success redirect on target page

    点赞 评论 复制链接分享