0

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');
5
  • Did you say console.log displays your count_ci value correctly in the success callback? Commented Jan 17, 2018 at 19:46
  • @TonyM: Yes, it does. I can't figure out how the value is correctly displayed in the console but isn't in the $_POST variable. Commented Jan 17, 2018 at 19:55
  • I know this might like a silly question: but how do you know it doesn't? echo $k; seems to be working - are you trying to access $_POST['count_ci'] ) outside of your Ajax handler function? i.e foo_item_count() Commented Jan 17, 2018 at 20:32
  • @TonyM: What you're asking is helpful--I am trying to access/echo the $_POST variable elsewhere (outside of the handler function), but haven't been able to. Commented Jan 17, 2018 at 20:58
  • posted an answer @chowwy, I had this exact problem a while ago Commented Jan 17, 2018 at 21:19

1 Answer 1

1

I had the exact same question/problem a few years ago. Turns out you cannot access that Ajax sent value anywhere else within WP besides your functions.php and of course plugin files. Reason being that while doing Ajax, WP only reloads the mentioned files (functions and plugins) but not the rest of the template structure. Therefore, while $_POST has your data, you can't render it on a template file because those are already loaded.

Edit:

Even within the plugin files, you only have access to your $_POST values within an Ajax handler function.

I recommend rethinking your goal, maybe Ajax isn't the way to go.

Sign up to request clarification or add additional context in comments.

5 Comments

I actually want to grab the $_POST variable in a plugin template. So are you saying it should be available there?
I might have been unclear - technically yes but STILL within the handler function. It is not available anywhere else. I'll update my answer to clear that up.
Ok, so how can I get the variable within the handler function? I've already echoed it out. If I run do_action('ajax_handler_function') it's still not pulling the variable.
I'm not sure what your end goal or the purpose of sending the value is but, you have it: $k. You can do operations on it, save it to the db, e.t.c and then echo it. e.g you can run query passing $k as an argument for a post_id or something. Whatever you're doing has to be done within foo_item_count() { }
Thanks for your help. I've accepted and upvoted your answer. I ended up saving the data to the database in my handler function and accessing it that way. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.