以DIV显示外壳输出

If I run this code in a php page I get the output as individual lines on my web page. Each line is displayed as the output is generated. This works fine and is what I expected.

do.php

$order   = array("
", "
", "");
$a = popen('rsyncd -Pav remote.server.com::files/file1.tar.gz localfile.tar.gz', 'r');

while($b = fgets($a, 64)) {
    ob_flush();flush();
        $update = str_replace($order,'<br />', $b);
        echo $update;
    ob_flush();flush();
}
pclose($a);

Is there any way I can call this via a button click and have the output written to a DIV. Ideally as each line is outputted it overwrites the previous line in the DIV. ( so the DIV only contains one line and that is constantly updated. )

I have tried this :

<script type="text/javascript">
$(document).ready(function() {
    $.ajaxSetup({ cache: false });

    $("#btn").click(function () {
        $("#log").load("do.php");
        });

});
</script>
<button id="btn">GO</button>

<div id="log" class="log"></div>

This works, but I only get all of the output in one go, not each line at a time overwriting the previous line.

I'm assuming I need to use an ajax call, but how do I do this and get each line of output as it is generated ?

Thanks

UPDATE

Thanks - I've followed the advise in one of the links and I am getting some live data back, but it isn't plain text. -

In plain text it should read :

receiving file list ...

1 file to consider

wrote 102 bytes  read 119 bytes  442.00 bytes/sec

total size is 71424169  speedup is 323186.29  

what I'm getting is :

receiving file list ... <br />2����+$��g��A���

*/�/IU040RH�,I-VP(JMLQ04���

M����!<���d�6��

*�/I�Q(άJU�,V0741214�TP(.HMM)-  Z��YBt��ⲷ��<ũ٢ 

anyway to get it as plain text. ?

This is the code I'm testing :

<script type="text/javascript">
        var last_response_len = false;
        $.ajax('./flushed-ajax.php', {
            xhrFields: {
                onprogress: function(e)
                {
                    var this_response, response = e.currentTarget.response;
                    if(last_response_len === false)
                    {
                        this_response = response;
                        last_response_len = response.length;
                    }
                    else
                    {
                        this_response = response.substring(last_response_len);
                        last_response_len = response.length;
                    }
                    console.log(this_response);
                }
            }
        })
        .done(function(data)
        {
            console.log('Complete response = ' + data);
        })
        .fail(function(data)
        {
            console.log('Error: ', data);
        });
        console.log('Request Sent');
        </script>