I want to periodically check the status of a php script and display it on the browser. Here is the jQuery that is used to post.
$(document).ready(function() {
$("#tempbut").click(function(event){
$('#loader').show();
$('#mainarea').hide('fast');
$.post(
"template.php",
{
guide : $('#guide').val(),
title : $('#title').val()
},
function(data) {
$('#mainarea').empty();
$('#loader').fadeOut('slow');
$('#mainarea').append(data);
$('#mainarea').show('slow');
}
);
});
});
In the PHP Script (not posted), I echo statements such as "Building request...", "Sending request..." to inform the user of the progress. If I were not using jQuery post, I would be able to show the status using flush() and obflush() after every echo. Perhaps it is because the callback waits for the script to fully execute before outputting?
Is there a way to modify the existing jQuery .post to do this? I see a few examples using .ajax, but not .post.
If .ajax is the only way to do this how would I modify this code?
Thanks!
EDIT: Got it running! Hopefully someone will find this helpful. (you can delete console.log)
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>TEST</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script>
$.ajax({
url: "test.php",
xhrFields: {
onprogress: function(e) {
console.log(e.target.responseText)
$("body").html(e.target.responseText)
if (e.lengthComputable) {
console.log(e.loaded / e.total * 100 + '%');
}
}
},
success: function(text) {
console.log(text)
$("body").html(text+"<h1>done!</h1>")
}
});
</script>
</head>
<body>
<div id="derp">TEST!</div>
</body>
</html>
test.php
<?php
for($i = 0; $i<20 ; $i++){
sleep(1);
echo $i."</br>";
flush();
ob_flush();
}
?>