views:

35

answers:

2

I am running a shell script in the background and redirecting the output to a log file in php. I want to display the contents from the log file on the page. I am able to do that using the code below.

<?php   
    $logfile = "hello";  
?>
function displayOutput()
{
    var html = <?php
        echo filesize($logfile)
            ? json_encode(file_get_contents($logfile))
            : '"Log file is getting generated"'; 
        ?>;
    document.form.text1.value = html;
}

However, the log file keeps updating till the script completes executing. How can i reload the updated contents from the file on the same page?

A: 

You need to set an interval timer to call your function every n seconds. Have a look at this answer to help you out - http://stackoverflow.com/questions/1350446/how-to-schedule-ajax-calls-every-n-seconds.

setInterval(displayOutput, (10 * 1000));
// reload log contents every 10 seconds
Martin
A: 

Maybe what you want basic XMLHttpRequest usage.

I am not really php guy, neiter javascript guru, just trying to give you an idea

function refreshText()
{
    if (window.XMLHttpRequest)
    {
        xhttp = new XMLHttpRequest();
    }
    else // for IE 5/6, just in case
    {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET","/page.php?action=download_log_file", false);
    xhttp.send();

    document.form.text1.value = xhttp.responseXML;
}
setInterval(refreshText, (10 * 1000)); // refresh text1.value every 10 seconds

same thing using jQuery

setInterval(function {
        $.get('/page.php?action=download_log_file', function(data) {
            $('#text1').val(data);
        });
}, (10 * 1000));

The handler script at the server just prints file data, see http://www.w3schools.com/xml/xml_server.asp for example

jonny