Update page while PHP script executes

If you have a script that runs for a long time and you would like to
update the browser with some progress reports here is a very simple way
of doing it:

script.php:

<html>
<body>
<div id="messages"></div>
</body>
<?php
function w($message){
?> <script language="javascript">
document.getElementById('messages').innerHTML =
document.getElementById('messages').innerHTML + "<?=$message;?>";
</script> <?php
@ob_flush(); @flush();
}
//example:
w("Here is a message");
sleep(5);
w("And another message");
sleep(5);
w("And yet another message");
?>
</html>

 

View an example here: click

Leave a Reply

Your email address will not be published. Required fields are marked *