In WordPress, I'm trying to post a jQuery variable using AJAX to use later in PHP. I've setup my jQuery function and the function to echo the variable.
I am getting the success message from the jQuery function in the console log, but the $_POST variable is null.
Below are the functions I've setup:
function foo_carousel_js() { ?>
<script>
jQuery(document).ready(function($) {
var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>";
var count_ci = $('.selector li').length;
$.ajax({
type: 'POST',
url: ajax_url,
data: {
action : 'foo_item_count',
count_ci : count_ci
},
success:function( data ) {
console.log( data );
}
});
});
</script>
<?php }
add_action( 'wp_footer', 'foo_carousel_js' );
function foo_item_count() {
$k = esc_html( $_POST['count_ci'] );
echo $k;
wp_die();
}
add_action('wp_ajax_foo_item_count', 'foo_item_count');
add_action('wp_ajax_nopriv_foo_item_count', 'foo_item_count');