I'm creating a plugin that at a certain point has to use AJAX to pass information to php depending on the events fired by front end user on the different page.

The event itself is when the user clicks on one of the links.

my ajax code is :

  $.ajax({  
         cache: false,
         async:false,
         timeout: 8000,
         type: 'POST',
         data: {action: 'getpostfilter', year : year, month: month, category: category},
         url: ajax_object_1.ajaxurl,
         success: function(data) {alert( "Handler for .click() called." + category+year+month );},
         error: function() {}

    });

I have created action hooks and the function that ajax calls:

function get_post_filter_by_date_and_category(){

     global $wpdb, $information;

     $information['category']= $_POST['category'];
     $information['year']= $_POST['year'];
     $information['month']= $_POST['month'];
        print_r($information);

    return $information;

    die();
}

    add_action('wp_ajax_getpostfilter','get_post_filter_by_date_and_category');
    add_action( 'wp_ajax_nopriv_getpostfilter','get_post_filter_by_date_and_category');

Where global $information was created in the beginning of the plugin source code is used to store the variables that are passed on to the php.

I call the function that supposed to get the post information on the separate page as follows:

get_post_filter_by_date_and_category();

if(isset($information)){

        echo('<h1> ELSE</h1>');

        print_r($information);

I didn't forget to properly enqueue the scripts and I also use wp_localize_script to pass admin-ajax.php as URL to ajax call.

I have absolutely no clue what is going wrong.

I have checked the following using different print and alert functions :

  1. The jQuery I am using is correct and creates all the variables and passes them to AJAX as intended.
  2. AJAX gets all the info ( checked through alert() ) and doesn't throw any errors
  3. PHP code runs without any 'blunt' errors . print_r() is used to check the contents of $information and every time it returns this :Array ( [category] => [year] => [month] => )

I am pretty sure this is not because I am not doing the extra security measures (nonce and etc.)

Please help.

  • The parameter data in your success function in your ajax request is going to contain what's printed out in your ajax php. Remember, this is a separate request to the server, so returning something from the php function won't do anything, you need to echo (or print_r, or whatever) it out because the output of the request is what's going to be returned to you. Also, because it's a separate request, it's not going to modify the global as you desire (it would modify it only for the ajax request itself). What do you get if you console.log(data) in your success function? – Andrew Bartel Jul 29 '14 at 17:25
  • I instead used the alert function to reproduce same results.. I get an aray with all the variables being correct – dev3078 Aug 6 '14 at 15:46
  • Moreover AJAX thinks that it has successively POSTED the data, whereas the php function doesn't detect any related variable in the $_POST (used isset function on $_POST['category'] to check that) – dev3078 Aug 7 '14 at 14:47

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.