0

I am using wordpress ajax and following code not passing parameter value metakey: id to $_POST["metakey"]. So var_dump($_POST) shows array(0) { } and $_REQUEST shows array()

if I enter static value of variable in PHP function $key=<any meta key> then its works fine

jQuery(".selectbox").change(function(){
     var id = this.id;

     // do a POST ajax call
    $.ajax({
      type: "POST",
      url: '<?php echo admin_url('admin-ajax.php'); ?>',
      data: ({
        action: "get-mata-value",        
        metakey: id
      }),
      success:  function( response ) {     
                jQuery.each(response ,function(index,value){
                    jQuery('#' +id).append('<option value="'+value+'">'+value+'</option>');
                });
 }
    });




});

PHP:

add_action("wp_ajax_get-mata-value", "get_mata_value");
add_action("wp_ajax_nopriv_get-mata-value", "get_mata_value");
function get_mata_value()
{   
    global $wpdb;
$key=$_POST["metakey"];
    $result=
 $wpdb->get_col( $wpdb->prepare( 
    "
    SELECT      DISTINCT meta_value
    FROM        $wpdb->postmeta 
    WHERE       meta_key = %s               
    ",
    $key
) ); 
 return($result);

    exit();

}

EDIT:

Under chrome developer tool I see

jquery.js?ver=1.12.4:4 XHR finished loading: POST

with following error:

jquery.js?ver=1.12.4:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in

  • Looks like you missed the post in js. – Nathan Powell Jan 21 '17 at 7:10
  • Where did you find this method? – Nathan Powell Jan 21 '17 at 7:12
  • You JavaScript is screwed. Do like one or two google searches about "Ajax WordPress" and you'll get a great answer for the JAVASCRIPT side of this. – Nathan Powell Jan 21 '17 at 7:18
  • I have did a lot and also tried $.post and jquery.post but same issue. Please provide me solution as answer – Muhammad Muazzam Jan 21 '17 at 7:20
  • 1
    You can check out my in depth answer about AJAX load in wp on SO here ;) – dingo_d Jan 26 '17 at 9:09
3

First of all you should read the codex on AJAX_in_Plugins

Secondly you should look at wp_localize_script to get the value for the admin-ajax url to your javascript.

$data = array(
    'ajax_url' => admin_url( 'admin-ajax.php' )
);

wp_localize_script( 'ajax-script', 'ajax_object', $data );

In your javascript, you are then meant to reference the localized data

jQuery(".selectbox").change(function(){
    var id = this.id;

    // do a POST ajax call
    $.ajax({
        type: "POST",
        url: ajax_object.ajax_url,
        data: ({
            "action": "get-mata-value",
            "metakey": id
        }),
        success:  function( response ) {

        }
    });
});
  • All these methods have been tried, the only issue is parameter value is not being passed. Ajax is working if I put static value in PHP – Muhammad Muazzam Jan 21 '17 at 8:46
  • Have you looked at wp_send_json? – Tunji Jan 22 '17 at 17:55
  • Getting error chrome developer tool: jquery.js?ver=1.12.4:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in – Muhammad Muazzam Jan 23 '17 at 6:36
  • I've updated the jquery code, but as you can see, it's just a pointer as to how you localize and get the ajax_url and not a correction on what you have. – Tunji Jan 23 '17 at 7:43
  • Have you localized the admin ajax url, is it available on the frontend, test with console.log – Tunji Jan 23 '17 at 7:43
0

I tried your code. Not sure why, but this.id doesn't return value for me. So instead try using jQuery(this).attr('id').

Also if this doesn't solve your problem, try console.log(id) before your ajax call and check what value you get.

  • I got id value on alert box – Muhammad Muazzam Jan 23 '17 at 11:30
  • Do you have a test link for your site or are you doing this on local machine? – Aniruddha Gawade Jan 23 '17 at 11:31
  • Data is being sent, have you tried using $_REQUEST instead of $_POST? In your PHP, print_r($_POST) and print_r($_REQUEST) – Aniruddha Gawade Jan 23 '17 at 11:54
  • print_r($_POST) Is showing array() that means parameter values are not being passed. It is the issue of this thread – Muhammad Muazzam Jan 23 '17 at 12:00
0

You are receiving an error in the jQuery plugin. I don't know why, but it seems to be the roadblock on your development. I looked at my wordpress installation, and I use the same version of jQuery, but without the error.

I don't know if there is a plugin or theme that is causing a conflict. The usual remedy is to disable all plugins and themes and see if the problem goes away.

You might also try enqueueing older versions of jQuery (if that's possible) to see if that clears up the problem.

Perhaps it's the installation of wordpress that is broken. Could you do a fresh install of WP somewhere else? Your local machine might be a good candidate.

I'm sorry I don't know why you're getting this error, it just seems clear that until you clear that up, you won't be able to see if your AJAX is working or not.

EDIT: I'm wondering if the error is occurring while the data is being sent, or when the data is being returned. In get_mata_value(), instead of "return($result)", try "echo $result;". And instead of "exit();" try "wp_die();". But instead of $result coming from the database, set it to a test value, like $result = "I get by with a little help from my friends";

I googled the error message from jQuery. See the last answer on this page. I would change the first line of the change callback function from var id=this.id; to var id=jQuery(this).id;

  • var id=jQuery(this).id; shows no value – Muhammad Muazzam Jan 26 '17 at 8:10
0

The issue was with google api as stated in question by showing api related error message

jquery.js?ver=1.12.4:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in

So I have added following code snippet in theme functions.php that called previous version of google api. It has solved the issue of passing parameter value. All other code was already fine.

  //Making jQuery Google API

    function modify_jquery() {

        if (!is_admin()) {

            // comment out the next two lines to load the local copy of jQuery

            wp_deregister_script('jquery');

            wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js', false, '1.8.1');

            wp_enqueue_script('jquery');
        }

    }

    add_action('init', 'modify_jquery');

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.