I am attempting to implement a live polling of a php script loop with no luck thus far in my attempts. Here is what I have thus far:
On form submit:
$.ajax({
data: $(this).serialize(),
success: showResponse,
url: 'process.php',
type: 'post'
});
function showResponse(){
$.ajax({
type: "GET",
url: "progress.php",
cache: false,
success: function(data) {
var response = $.parseJSON(data);
if (response.processing === true) {
console.log("Current Item: " + response.currentItem +
"Total Items: " + response.totalItems +
"Percent Complete: " + response.percentComplete);
setTimeout(checkProgress, 1000);
});
}
In the process.php script:
session_start();
echo json_encode(array("processing" => true));
$totalItems = 10000000;
$_SESSION['totalItems'] = $totalItems;
$_SESSION['processing'] = true;
$_SESSION['error'] = false;
for ($i=0; $i <= $totalItems; $i++) {
$_SESSION['currentItem'] = $i;
$_SESSION['percentComplete'] = round(($i / $totalItems * 100));
}
In the progress php script:
session_start();
echo json_encode(array(
"processing" => $_SESSION['processing'],
"error" => $_SESSION['error'],
"currentItem" => $_SESSION['currentItem'],
"totalItems" => $_SESSION['totalItems'],
"percentComplete" => $_SESSION['percentComplete']
)
);
Not sure where I am going wrong here but all it does is loop once it hits 100% complete. Any suggestions would be greatly appreciated!
EDIT I changed the above to using apc in the process.php:
apc_store('totalItems', $totalItems);
apc_store('processing', true);
apc_store('error', false);
apc_store('currentItem', $i);
apc_store('percentComplete', round(($i / $totalItems * 100)));
And within the progress.php:
echo json_encode(array(
"processing" => apc_fetch('processing'),
"error" => apc_fetch('error'),
"currentItem" => apc_fetch('currentItem'),
"totalItems" => apc_fetch('totalItems'),
"percentComplete" => apc_fetch('percentComplete')
)
);
Still doesn't work properly the way I am wanting it to work, am I doing something incorrectly? It only shows false values until the script completes and shows 100% just like the session use was doing before. Any ideas?