I’m making an attempt to get an ajax request to replace the response because it goes by means of a loop, just like a progress bar.
A little bit of background on this snippet. Right here is the JS Ajax request:
// ------------initiate ajax name to generate database entries------------
var ajax_data = {};
ajax_data.motion = 'database_generate';
// ------------run ajax name and return response------------
jQuery.ajax({
technique: 'submit',
url: '/wp-admin/admin-ajax.php',
information: ajax_data,
success: operate(response){
response_container.innerText = response;
}
});
The javascript is fairly easy “success” response. I believe that this can be a part of the difficulty as to why it wont return till the loop finishes. I do not know how you can name it otherwise to replace.
Right here is the PHP Snippet:
// ------------loop by means of grouped CSV data------------
foreach ($return_arrary as $current_post) {
// ------------check if present state exists in database------------
if (!$post_exists($current_post['State'])) {
// ------------create submit data------------
$post_array = array(
'post_title' => $current_post['State'],
'post_type' => 'jed_map_states',
'post_status' => 'publish'
);
// ------------insert submit into database------------
wp_insert_post($post_array);
$counter++;
}
}
echo $counter." States added to database.";
The php is wants a tad extra context. $return_array is a listing of state names which can be grabbed from a csv file. $post_exists is $wpdb name to test if a submit with the identical title has been created. $counter is initialized earlier within the script. If I place the echo within the foreach loop it solely sends an enormous response of every iteration after the loop finishes ie:
0 States Added
1 States Added
2 States Added
…and so on and so on
I believe that it takes a bit because it has to make the $wpdb name each time on loop. I’d be nice if there was a real-time replace as its working.
Any assist can be drastically appreciated!