1

I'm running a PHP script that accesses some MySQL databases, and I need to wait a few seconds until the previous script has entered all the information into the database. The wait time shouldn't be long, but just in case servers are slow on an off day, I'm using sleep(10) to wait 10 seconds before executing the script. I wanted to display a "please wait" message, while it is waiting that 10 seconds, but unfortunately that message gets displayed only after the 10 seconds has already been completed. This is the way I'm going at it:

ob_start();
echo "Please wait while your invoice is being created... </br>";
ob_flush();
sleep(10);
ob_end_clean();
echo "Success...";

Based on some research online, I was under the impression that ob_flush() would output the text before the 10 seconds was up, and then ob_end_clean() would erase the previously printed text, but what I'm getting instead is that both texts are being displayed after the 10 seconds is up and the first text is not being erased. Do you guys know what I'm doing wrong here?

user1562781
  • 379
  • 1
  • 9
  • 20
  • 1
    If you want to modify your html after it has been sent to the browser, you need to use javascript, you cannot do that with php. – jeroen Aug 16 '12 at 20:54
  • Do you have output buffering on webserver ? – Zaffy Aug 16 '12 at 20:56
  • No, I'm not modifying any html. All this PHP script is to do is to echo information from several different MySQL databases. – user1562781 Aug 16 '12 at 20:56
  • Note that the output buffer is not what you are seeing in the console / browser, it is just some memory that php is using. – jeroen Aug 16 '12 at 20:59
  • I'm reading the PHP manual on ob_flush() and it says that browsers are treating output buffers as an all or nothing object, so ob_flush() cannot be used to display text prior to sleep(). Is there no workaround to this? http://php.net/manual/en/function.ob-flush.php – user1562781 Aug 16 '12 at 21:06
  • If you are accessing your script via the browser, just use ajax. Then you don't have to use a timeout, you can display your success message directly after the script / db operations finish. – jeroen Aug 16 '12 at 21:15

1 Answers1

0

You cannot edit anything you have outputted already with PHP, that would require a page reload. ob_flush just outputs the buffer and empties it. ob_end_clean() stops the output buffering, allowing you to output normally again.

To hide shown text you would output a javascript to hide the previously rendered text (preferably wrapped in a div or so for ease of selecting).

Clarence
  • 2,944
  • 18
  • 16
  • Is there any way to at least output the text before the 10 seconds is up? – user1562781 Aug 16 '12 at 20:59
  • I did not realize that was not already working. Call ob_flush(); flush(); instead to increase the chances of that. There are a few gotchas at that. Turning of GZIP compression makes it likelier that the web server will not buffer it, and outputting more than 255 characters makes it more reliable in IE (which at least used to empty the buffer at that point). – Clarence Aug 17 '12 at 06:51