0

I have written following js code

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

     jQuery.post(
    // see tip #1 for how we declare global javascript variables
    MyAjax.ajaxurl,
    {
        // here we declare the parameters to send along with the request
        // this means the following action hooks will be fired:
        // wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
        action : 'get-mata-value',

        // other parameters can be added along with "action"
        prev_metakey : jQuery(this).val(),
        metakey: key_id
    },
    function( result ) {
       if(result['success'] != false)
        {
            jQuery.each(result,function(index,value){
                jQuery('#' +id).append('<option value="'+value+'">'+value+'</option>');
            });
        }
    }
);

});

and function is:

wp_localize_script( 'script', 'MyAjax',
    array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
    )
);
wp_enqueue_script( 'script', get_template_directory_uri() . '/js/vehicle_parts.js', array ( 'jquery' ), 1.1, true);


    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"];
        $prev_value=$_POST["prev_metakey"];
        $result=array();

     $result=$wpdb->get_col("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '$key'");

     return($result);

    }

when I change select box the function is not being called.

  • Are you enqueueing your script and localizing ajaxurl? Do you see any errors in your browser console? – Welcher Jan 16 '17 at 14:14
  • yes it has been enqueueing. only php function not being called – Muhammad Muazzam Jan 16 '17 at 14:19
  • Can you share the enqueue code as well? – Welcher Jan 16 '17 at 14:20
  • wp_enqueue_script( 'script', get_template_directory_uri() . '/js/vehicle_parts.js', array ( 'jquery' ), 1.1, true); – Muhammad Muazzam Jan 16 '17 at 14:21
  • 1
    There is a reference to MyAjax.ajaxurl - where is the being localized? – Welcher Jan 16 '17 at 14:27
0

Based on your comments, I don't think the MyAjax variable is being localized using wp_localize_script().

In your case, you'll most likely need something like the following:

wp_localize_script( 'script', 'MyAjax',
    array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
    )
);

That said, I don't really have enough information to provide a more detailed answer.

EDIT: This and the enqueue scripts call should be inside of the wp_enqueue_scripts callback:

add_action('wp_enqueue_scripts, 'enqueue_my_scripts' );
function enqueue_my_scripts() {

    wp_enqueue_script( 'script', get_template_directory_uri() . '/js/vehicle_parts.js', array ( 'jquery' ), 1.1, true);

    wp_localize_script( 'script', 'MyAjax',
        array( 
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
        )
    );

} 
  • still function not being called – Muhammad Muazzam Jan 16 '17 at 14:42
  • Where are you adding this code? It should be in the same callback that enqueues the script. I really can't help much more without more details. Are you seeing any console or network errors in your console? – Welcher Jan 16 '17 at 14:44
  • added code in question – Muhammad Muazzam Jan 16 '17 at 14:46
  • You should be adding the enqueue and localization code inside a callback added to the wp_enqueue_scripts action – Welcher Jan 16 '17 at 14:48
  • I have still issue with function call – Muhammad Muazzam Jan 18 '17 at 13:46
0

as per code codex you should localize after enqueueing your javascript.

IMPORTANT! wp_localize_script() MUST be called after the script has been registered using wp_register_script() or wp_enqueue_script().

Edit: you need really to echo your output from your ajax callback and use exit() or die() after that.

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.