I have a form that currently is processed by an action in a PHP script when submitted. The PHP script contains a number of procedures that take time to process. I'd like to print to the parent script (the one with the form, which remains visible on screen until the PHP script is finished) status reports from the PHP script, e.g.:
Procedure 1 has begun... Procedure 1 is finished.
Procedure 2 has begun... Procedure 2 is finished.
Procedure 3 has begun... Procedure 3 is finished.
etc.
I understand that the primary way to do this is by intercepting the form submission with an AJAX call, like this:
$('#formID').on('submit',function(e){
e.preventDefault();
$.ajax({
type : "POST",
cache : false,
url : $(this).attr('action'),
data : $(this).serialize(),
success : function(data) {
// do something with the returned data from the PHP script
}
});
});
However, that would seem just to return/report the return value (echo message, etc.) from the PHP file that is called by the form. I want a series of messages to be returned to the parent form indicating the progress of the PHP script as it processes values from the form.
For reasons I don't fully understand, the form page remains visible until the PHP action is completed, at which point the page is redirected to a different page in the site by use of a header() directive at the end of the PHP script. (The code was originally written by a different developer; my understanding of his routine is imperfect. The submit() is called within a JQuery PLUpload function, which may or may not be why the form page remains on screen until the PHP action script is finished.)
Without breaking up the PHP form processing into a series of individual scripts, each processed separately, how can I use AJAX (or another technique) to intercept the default form processing and execute the PHP processing script such that status text messages may be echoed to the screen or appended as styled text on <div>
elements in the form page? (The PHP processing script relies on a series of variables that are derived from the values on the form, and it would be inelegant, to say the least, to have to recreate them in a succession of individual PHP files called sequentially, since the current PHP form processing script is efficient and works fine, albeit slow because of the nature of its operations.)
On my localhost, and perhaps on many live servers, the solution requires a header <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
followed by while (@ob_end_flush());
and then an explicit flush();
after each text echo. A simple example of this would be this short routine:
<?php
header( 'Content-type: text/html; charset=utf-8' );
while (@ob_end_flush());
function sendMsg($msg) {
echo $msg;
flush();
}
function sleep_test($sleep_time) {
$x = "Beginning process:<br>";
sendMsg($x);
$y = "Waiting $sleep_time seconds ...<br>";
sendMsg($y);
sleep($sleep_time);
$z = "Done.<br><br>";
sendMsg($z);
}
sleep_test(1);
sleep_test(2);
sleep_test(3);
?>
However, I'm on a shared hosting plan on my live server whose admin settings (i.e., those I cannot alter via php.ini and Apache overrides, etc.) will not display its text until it is entirely processed; only then will the buffer be fully flushed. The script above works perfectly on my XAMPP and WAMP localhosts, and should for anyone else's, but if it doesn't and you're trying to get this kind of performance on your live host, then if this script doesn't flush the buffer until it is fully processed, it may not be possible; contact your liveserver's admin for info.
For my purposes, a very carefully constructed set of AJAX calls worked for the processing script from which I desired status text updates as the processing proceeded.