Php long running script to dynamically update html

This code is a work in progress and should never be used in a production environment without extensive testing!

First, the page that is loaded into the browser, it uses jQuery to receive status updates from ‘getstatus.php’ script

<title>Update Inventory</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="" type="text/javascript"></script>
<script type="text/javascript">
     var flag = false
     var stop = false;
$(function() {
function startProcess() {
    $("#done").load('updatestock.php', function() {
        flag = true;
function getStatus() {
    if ( stop ) {return false;}
	if ( flag ) {stop = true;}
<style type="text/css">
div {padding:0 0 0 50px;}
	<h1>Long Running Script</h1>
	<div id="done"></div>
	<div id="status"></div>

This is the long running script simulator

$fh = fopen('status.log', 'w'); 
//simulate work for 30 seconds
    while($i < 30) {
	fwrite($fh, time()."<br />\n");

And getstatus.php, the script that returns data to be appended to div

this script is accessed by the html version of the updater, and spools the output to the screen 
so the user can see updated information as the the script runs.
$page = file_get_contents('status.log');
echo $page;

Try it here (buggy)

2 thoughts on “Php long running script to dynamically update html”

  1. I like the approach..but I don’t know why it won’t create the file until the loop finishes, even if I open and close the file inside the loop

    Thanks for sharing!

Leave a Reply

Your email address will not be published.