I am trying to print a report in a textArea using Javascript or JQuery as i want to show user what is happening while a PHP script is processing in background for some time, so the user won't think the application hanged.
So i tried to automatically update the textArea using setTimeout(...), and set the report value in the session through the PHP script while it is processing to get that variable in the client side and print it in the textArea.
Here is the code i used :
/*
* The client side javascript function that updates the textArea value.
*/
function updateReport() {
var reportValue = '<?php echo( $_SESSION[ 'report' ] ); ?>';
document.forms[ 'start_send_form' ].elements[ 'reportTextArea' ].value = reportValue;
setTimeout(function () {
updateReport();
}, 500);
}
The php script that is called by the client side through an Ajax(JQuery) request :
<?php
$report = "";
for( $i = 0; $i < $number_of_items_to_process; $i++ ) {
$report .= "- Processing a new item";
$_SESSION[ 'report' ] = $report;
// .... Process some long code here.
}
?>
But it seems that updateReport() executes just once when it is first called, then resumes when the PHP script is done.
Can you explain this for me ?
And is there a way to accomplish what i want ?
---------------------------------- >> Update: The code i use trying to get the updated value for $_SESSION[ 'report' ] from server side :
// 1. JavaScript :
function updateReport() {
$.ajax({
url: "test2.php",
type: "POST",
cache: false,
success: function( returned_value ){
var reportValue = $.trim( returned_value );
alert(reportValue); // All messages appear after PHP script ends.
}
});
setTimeout(function () {
updateReport();
}, 500);
}
// 2. test2.php(Script just made to get $_SESSION[ 'report' ]) :
<?php
if ( !isset( $_SESSION ) ) session_start();
echo( $_SESSION[ 'report' ] );
?>