To update the response as the loop progresses, you can try the following:
1- Modify the PHP script to output the current loop count after each iteration, rather than waiting until the end of the loop to output the final count. This will allow the success callback in the Ajax request to update the response as the loop progresses.
2- In the success callback, you can use the response_container.innerText property to update the response with each iteration of the loop.
For example:
// ------------loop through grouped CSV data------------
foreach ($return_arrary as $current_post) {
// ------------check if current state exists in database------------
if (!$post_exists($current_post['State'])) {
// ------------create post data------------
$post_array = array(
'post_title' => $current_post['State'],
'post_type' => 'jed_map_states',
'post_status' => 'publish'
);
// ------------insert post into database------------
wp_insert_post($post_array);
$counter++;
// ------------output current loop count------------
echo $counter." States added to database.";
// ------------flush output buffer------------
ob_flush();
flush();
}
}
// ------------initiate ajax call to generate database entries------------
var ajax_data = {};
ajax_data.action = 'database_generate';
// ------------run ajax call and return response------------
jQuery.ajax({
method: 'post',
url: '/wp-admin/admin-ajax.php',
data: ajax_data,
success: function(response){
// ------------update response with each iteration of the loop------------
response_container.innerText = response;
}
});
Note: You may also need to add ignore_user_abort(true); at the beginning of the PHP script to prevent the script from being terminated when the user navigates away from the page.
Another option is to use JavaScript's setInterval function to periodically send an Ajax request to check the progress of the loop. In this case, you can modify the PHP script to store the current loop count in a session variable or in a temporary file, and the success callback in the Ajax request can read this value and update the response accordingly.
For example:
// ------------initiate ajax call to generate database entries------------
function checkProgress() {
// ------------run ajax call and return response------------
jQuery.ajax({
method: 'post',
url: '/wp-admin/admin-ajax.php',
data: {
action: 'check_progress'
},
success: function(response){
// ------------update response with current loop count------------
response_container.innerText = response;
// ------------stop interval if loop has finished------------
if (response === 'Done') {
clearInterval(intervalId);
}
}
});
}
// ------------check progress every 1000 milliseconds (1 second)------------
var intervalId = setInterval(checkProgress, 1000);
In the PHP script, you can use a session variable or a temporary file to store the current loop count and return it in the response:
// ------------start session------------
if (session_status() === PHP_SESSION_NONE) {
session_start);
}
// ------------loop through grouped CSV data------------
foreach ($return_arrary as $current_post) {
// ------------check if current state exists in database------------
if (!$post_exists($current_post['State'])) {
// ------------create post data------------
$post_array = array(
'post_title' => $current_post['State'],
'post_type' => 'jed_map_states',
'post_status' => 'publish'
);
// ------------insert post into database------------
wp_insert_post($post_array);
$counter++;
// ------------store current loop count in session variable------------
$_SESSION['counter'] = $counter;
}
}
// ------------action to check progress------------
function check_progress_action() {
// ------------return current loop count------------
echo $_SESSION['counter']." States added to database.";
exit;
}
add_action('wp_ajax_check_progress', 'check_progress_action');
add_action('wp_ajax_nopriv_check_progress', 'check_progress_action');
Note: Don't forget to end the session at the end of the PHP script by calling session_write_close().
I hope this helps! Let me know if you have any questions or need further assistance.