Ajax / PHP更新DIV

I'm using the following to update a DIV called 'output'. This works fine with one exception, I would like echo entries to update the parent page.

<script type="text/javascript">
<!--
var divid = 'output';
var loadingmessage = '<img src="working.gif">';
function AJAX(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
function formget(f, url) {
var poststr = getFormValues(f);
postData(url, poststr);
}
function postData(url, parameters){
var xmlHttp = AJAX();
xmlHttp.onreadystatechange =  function(){
if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){
document.getElementById(divid).innerHTML=loadingmessage;
}
if (xmlHttp.readyState == 4) {

document.getElementById(divid).innerHTML=xmlHttp.responseText;
}
}

xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parameters.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(parameters);
}

function getFormValues(fobj)

{
var str = "";
var valueArr = null;
var val = "";
var cmd = "";

for(var i = 0;i < fobj.elements.length;i++)

{
switch(fobj.elements[i].type)

{

case "select-one":

str += fobj.elements[i].name +
"=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&amp;amp;";
break;

}
}

str = str.substr(0,(str.length - 1));
return str;

}

//--></script>

This is called using :

<input type='button' name='Send' value='submit' onclick="javascript: formget(this.form, 'foo.php');">

The issue I have is foo.php runs a series of exec() commands, between each command is an echo statement that I would like to be displayed in the output div.

So it will do something like:

echo "archive files"; exec ("tar -cvf bar.tar bar.txt foo.txt");

echo "backing up /user"; exec ("tar -cvf /user.tar /user/*");

I would like the user to see the working.gif, but under it each echo statement from foo.php Can that be done and how ?

Thanks

I can't say I've ever tried sending back chunks of data at separate times with a single AJAX request, so I'm not sure it's possible. What happens currently? Do you only get first echoed message, or do only get the entire response at the end?

Two things that I know will work:

  1. Break your PHP script into multiple scripts and execute them in order with separate AJAX requests. This will only work if the separated scripts don't depend on each other or you find some other way to persist the state across the separated scripts.

  2. Create an iframe and load the PHP script into it instead of using an AJAX request. Flushing the output of the PHP script should then work. (If you have ever used Wordpress, I believe they use this technique to show the progress of plugin updates.)