Search code examples
javascriptphpajaxlivedisplay

Live displaying PHP output on a webpage


I would like to display the output of my PHP script in a div in live. I'm using Laravel 5.4 My PHP code :

$cmd = 'ping -c 10 127.0.0.1';

while (@ ob_end_flush());
$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
    echo fread($proc, 4096);
    @ flush();
}
echo '</pre>';

return view('layouts/test');

When I load the page, my function work and it's live displaying but it's take up the whole page. I tried to use Ajax, I was able to retrieve the output but it's not live displaying. Someone have an idea ?


Solution

  • Loading the script in an iframe should work.

    <iframe src="/script.php"></iframe>
    

    Or alternatively if you need it to load after a specific action

    <iframe id='myIframe'></iframe>
    
    <script type="text/javascript">
    document.getElementById('myIframe').src = '/script.php';
    <script>