WordPress.org

Ready to get started?Download WordPress

Forums

WordPress AJAX onchange event help (2 posts)

  1. bhattigurjot
    Member
    Posted 1 year ago #

    I've drop down select menu which selects the branch. It is not in the form.

    And I have included a action for it:

    add_action( 'wp_ajax_the_ajax_hook_2', 'display_data_table' );

    With the function that deals with database:

    function display_data_table(){
    
    $mno=$_POST['branch'];
    
    //.. more code
    
    }

    Also my drop down menu html coding is:

    <select name="branch" id="sel_branch" >
    
    <option value="CSE">CSE</option>
    
    // more options..
    
    </select>

    Now my jQuery script is like:

    jQuery(document).ready(function(){
    
    jQuery("#sel_branch").on('change', function(){
    
    var dropdown_id=$(this).val();
    
    jQuery.ajax({
    
    type : "post",
    
    url : the_ajax_script.ajaxurl,
    
    data : {
    
    action: "the_ajax_hook_2",
    
    branch : dropdown_id
    
    },
    
    success: function(response_from_display_data_table) {
    
    jQuery("#table_disp").html(response_from_display_data_table);
    
    }
    
    });
    
    });
    
    });

    But it is not working. Am I doing something wrong here? Did I miss something?

    @all.. if anyone's having any idea then please help.

  2. ianhaycox
    Member
    Posted 1 year ago #

    Can't see anything obvious in the code but a couple of things to check.

    1. Ensure the add_action for the AJAX callback is not within an

    if (is_admin()) {...}

    block as it won't work.

    2. If you (or the user) are not logged in to WP, then you will also need the nopriv variant as well

    add_action( 'wp_ajax_nopriv_the_ajax_hook_2', 'display_data_table' );

    3. Verify the value in the JS for the_ajax_script.ajaxurl points to http://mysite.com/wp-admin/admin-ajax.php

    4. Using either the Firefox Firebug extension or Chrome developer tools fire up the developers console and inspect the XHR requests.

    Using the developers console you can see the AJAX requests and responses and verify the change event is firing and sending the values to your PHP function.

Topic Closed

This topic has been closed to new replies.

About this Topic