howto: please wait, this may take a few seconds...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erwin Kloibhofer

    #1

    howto: please wait, this may take a few seconds...

    what if i have a webpage that displays the text "please wait, this may take
    a few seconds..." and it now waits until some event on the server happens.
    whatever this is, this can be quick, but it could also be slow, or it can
    even fail. but once this event has happened, i want the webpage to reload
    and display a different message, like "the process completed successfully".

    my question now is, how would i achieve this without constant visible
    reloads? (which would probably be the simplest solution). is there a way to
    do invisible polling via maybe a javascript (somewhere i picked up that
    "ajax can do that")? what i want to achieve is that only if the event i was
    waiting for actually occured, i want the webpage to actually visibly
    reloaded, and displaying the new message.

    what approach do for instance professional payment systems take, while the
    client is waiting if the credit-card number is valid?

    can someone push me in the right direction?


  • badr

    #2
    Re: howto: please wait, this may take a few seconds...

    hi , Erwin Kloibhofer

    Actually i like your Quistion and i start to think how it could be ,
    but it is can be in different part
    1- you need to calculate php web page time execution.
    2- you have to biuld your own template that may diplay an error or
    status of submitted data if it is correct or not or waiting message.

    but there is alot of method to do so.

    i can give small code to execution time
    like this
    <!-- put this at the top of the page -->
    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
    $starttime = $mtime;


    <!-- put other code and html in here -->


    <!-- put this code at the bottom of the page -->

    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
    $endtime = $mtime;
    $totaltime = ($endtime - $starttime);
    echo "This page was created in ".$totaltim e." seconds";


    or simply see this http://www.developerfusion.co.uk/show/2058/

    so after you processing data you may need to check the time exeution
    and and pass it to an HTML META tag as a parameter to display the
    message that tell the user pleas wait a moment the META TAG may like
    this

    <META HTTP-EQUIV=\"Refresh \"
    CONTENT=\"$X_se c; URL=$result_pag e\">

    while $X_secis the time in second so the value of X will be got from
    the above php code
    and the HTML META tag may be reside on the top of the message template
    inside the <head> tag .

    and $result_page is the target page that go to next step if the data
    are OK or the page that abort the process

    test this page to for demo redirection


    ---------------------------------------------------------------
    anaother method you may need to create a function that display a
    message and wait for data process by other function if the returned
    value is valid you may go to next step or change the message if it is
    invalid display error message or redirect the user to previuos page
    ---------------------------------------------------------------

    i hope i give some Help
    Regard

    Comment

    • Sacs

      #3
      Re: howto: please wait, this may take a few seconds...

      Erwin Kloibhofer wrote:[color=blue]
      > what if i have a webpage that displays the text "please wait, this may take
      > a few seconds..." and it now waits until some event on the server happens.
      > whatever this is, this can be quick, but it could also be slow, or it can
      > even fail. but once this event has happened, i want the webpage to reload
      > and display a different message, like "the process completed successfully".
      >
      > my question now is, how would i achieve this without constant visible
      > reloads? (which would probably be the simplest solution). is there a way to
      > do invisible polling via maybe a javascript (somewhere i picked up that
      > "ajax can do that")? what i want to achieve is that only if the event i was
      > waiting for actually occured, i want the webpage to actually visibly
      > reloaded, and displaying the new message.
      >
      > what approach do for instance professional payment systems take, while the
      > client is waiting if the credit-card number is valid?
      >
      > can someone push me in the right direction?
      >
      >[/color]

      You could use the onload() function in the <BODY> to redirect once the
      page has finished, or use php to inject some javascript once your
      processing is done, or get a little clever with the CSS visibility
      property.

      <html>
      <head>
      <style>
      #waiting {
      width: 100%;
      height: 100%;
      }
      </style>
      <script language=javasc ript>
      function hide() {
      h = eval('document. getElementById( \'waiting\')');
      h.style.visibil ity = "hidden";
      h.style.height = "0%";
      }
      </script>
      </head>
      <body onload="hide(); ">
      <div id="waiting">
      <center><br>
      This may take a while!
      </center>
      </div

      <?php

      flush();
      echo "doing something...... <br>";

      sleep(10);

      echo "Done <br>";
      ?>

      Sacs

      Comment

      • Chung Leong

        #4
        Re: howto: please wait, this may take a few seconds...

        There are a number of ways to do an asynchronous look up through
        Javascript. If you need something sophisticated, Google the web for
        AJAX libraries.

        A simple, cross-browser technique I've used before involves using
        setInterval() to periodically call a function, which then dynamically
        creates a <SCRIPT> tag linking in a PHP-generated script file. This
        script file would contain just a call to a function already defined,
        passing updated information from the server as parameter.

        You could also do it without Javascript. Just print out a message,
        perform the time-consuming task, then output a CSS style retroactively
        to turn off the message.

        Example:

        <div id="test">
        please wait, this may take a few seconds...
        </div>
        <?

        // time consuming task

        ?>
        <style>

        #test {
        display: none;
        }

        </style>

        The trick here is in keeping the browser from timing out and proper
        buffer flushing.

        Comment

        • IWP506@gmail.com

          #5
          Re: howto: please wait, this may take a few seconds...


          Chung Leong wrote:[color=blue]
          > There are a number of ways to do an asynchronous look up through
          > Javascript. If you need something sophisticated, Google the web for
          > AJAX libraries.
          >
          > A simple, cross-browser technique I've used before involves using
          > setInterval() to periodically call a function, which then dynamically
          > creates a <SCRIPT> tag linking in a PHP-generated script file. This
          > script file would contain just a call to a function already defined,
          > passing updated information from the server as parameter.
          >
          > You could also do it without Javascript. Just print out a message,
          > perform the time-consuming task, then output a CSS style retroactively
          > to turn off the message.
          >
          > Example:
          >
          > <div id="test">
          > please wait, this may take a few seconds...
          > </div>
          > <?
          >
          > // time consuming task
          >
          > ?>
          > <style>
          >
          > #test {
          > display: none;
          > }
          >
          > </style>
          >
          > The trick here is in keeping the browser from timing out and proper
          > buffer flushing.[/color]

          You want a flush() after your div so it becomes visible. Otherwise, it
          defeats the purpose.

          <div id="test">
          please wait, this may take a few seconds...
          </div>
          <?
          flush();
          // time consuming task

          ?>
          <style>

          #test {
          display: none;
          }

          </style>

          Comment

          • Erwin Kloibhofer

            #6
            Re: howto: please wait, this may take a few seconds...

            thanks for all your input, it did help a lot!

            i came up with a piece of code that does exactly what i want. that is...

            o) send out a page with a "this may take a while..." message
            o) wait on the server until a certain event has happened
            o) once it did happen, redirect to a different page

            the only drawback i can see is that the browser keeps displaying "opening
            page bla". that is something i can live with and i guess any other solution
            that would avoid this would be too difficult to implement, where else the
            current solution seems elegant and simple.

            one problem i had to solve was to force the browser to do a full reload of
            that page, and not taking the page from the cache. is there a more elegant
            method to achieve this?

            can anyone see any other problems with this code? what i am concerned about
            is that after i sent the </body> and </html> tags i keep sending data...

            =============== =============== =============== =========

            <?php
            // create a header from the past to make sure the page does not get cached
            header("Expires : Mon, 01 Jan 1999 05:00:00 GMT");
            header("Last-Modified: ".gmdate("D , d M Y H:i:s")." GMT");
            header("Cache-Control: no-store, no-cache, must-revalidate");
            header("Cache-Control: post-check=0, pre-check=0", false);
            header("Pragma: no-cache");
            ?>
            <html>
            <body onload="locatio n.href='index.p hp'">
            This may take a while...
            </body>
            </html>
            <?php
            if (ob_get_level() == 0) ob_start();
            for ($i = 0; $i < 3; $i++)
            {
            echo "<b></b>"; // send some useless tags
            echo str_pad('', 4096)."\n"; // send at least a 4K buffer
            ob_flush();
            flush();
            sleep(2);
            }
            ob_end_flush();
            ?>


            Comment

            Working...