0

I need to push js variable into php variable. AJAX url is set via wp_localize_script but it returns ERROR 400 Bad Request. functions.php is looks like

wp_localize_script( 'script-js', 'compareids_ajax', array( 'ajax_url' => admin_url('admin-ajax.php')) );

custom.js

 var compareIDs = $(".table td input:checkbox:checked").map(function(){
          return $(this).val();
        }).get(); // <----
        $('.selected').text(compareIDs);
        console.log(compareIDs);



             $.ajax({
                        type: "POST",

                        url: compareids_ajax.ajax_url,
                        data: '{ compareIDs : compareIDs }',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {

                        },
                        error: function (errormessage) {

                        }
                    });

and my filter-page.php has

  <?php

                $compareIDs = array( $_POST['compareIDs'] );


    $args02 = array( 'post_type' => 'custom',
                     'post__in' => $compareIDs );

    $loop02 = new WP_Query( $args02 );

    while ( $loop02->have_posts() ) : $loop02->the_post();
    ?>
  • And where is the code that registers your hooks that will process that AJAX request? – Krzysiek Dróżdż Jun 14 '18 at 6:16
1

First you need to to send function name as additional data as

  $.ajax({
        type: "POST",
        url: compareids_ajax.ajax_url,
        data: {                             // Data object
              compareIDs : compareIDs,
              action: 'your_ajax_function'  // This is required to let WordPress know which function to invoke
        },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
              console.log( msg );
        },
        error: function (errormessage) {
              console.log( errormessage );
        }
  });

In your php file

  add_action('wp_ajax_nopriv_your_ajax_function','your_ajax_function'); // Ajax Function should be hooked like this for unauthenticated users
  add_action('wp_ajax_your_ajax_function','your_ajax_function'); // 

  function your_ajax_function(){

        $comparedIds = $_POST['compareIDs']; // Your data is now available in $_POST 
       // Do your stuff here

    die(); // At the end 

  }

See wp_ajax_(action)

0

Thank you. I actually did that but it seems there is no connection to the admin-ajax.php Its always 400 Bad request. Even if I just test it with the functions.php

          add_action('wp_ajax_my_action', 'my_action');

           function my_action(){

            echo 'My action';

          }


   then enter http://my-website.com/wp-admin/admin-ajax.php?action=my_action

the browser displays 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.