I am trying to poll a PHP page for an updated $val variable value, but for some reason it doesn't want to show anything on the calling html page
PHP (generator.php) script:
<?php
header('Content-type: text/html; charset=utf-8');
function output($val)
{
echo $val;
flush();
ob_flush();
usleep(500000);
}
output('Begin... (counting to 10)');
for( $i = 0 ; $i < 10 ; $i++ )
{
output($i+1);
}
output('End...');
?>
HTML page:
<html>
<head>
<script type="text/javascript">
function generate(){
try {
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
}
xmlhttp.onreadystatechange = refreshwait;
xmlhttp.open('GET', './generator.php', true);
xmlhttp.send('null');
}
function refreshwait() {
if (xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
var code = 'Finished';
document.getElementById('view_area').innerHTML = code;
}
}else{
document.getElementById("view_area").innerHTML = xmlhttp.responseText;
refreshwait();
}
}
</script>
</head>
<body>
<div id="a_form">
<form id="aform">
<input name="Generate" onClick="generate()" type="button" value="Generate Fresh File" />
</form>
</div>
<div id="view_area"></div>
</body>
</html>
What am I doing wrong? I am expecting to see a count from 1 to 10 appear on the html page as the php is running.