The WordPress Ajax request returns 0

I can’t figure out why nothing is returned
I am a real beginner in Ajax ..
I just read a lot of topics about using Ajax in Woprdpress but the examples are super advanced for me
Here is my JS code combo_checkout_iRange.js

jQuery(document).ready(function() {
    jQuery('#loader').hide();
    jQuery('#check-out-date').hide();
    jQuery('#check-in-date').change(function(){
        jQuery('#check-out-date').fadeOut();
        jQuery('#loader').show();
        jQuery.ajax({
            type: 'POST',
            url:myAjax.ajaxurl, 
            data: {
                action : 'my_ajax_handler',
                parent_id: jQuery("#check-in-date").val()
            },
            success: function(data){
                alert(data);
                jQuery('#loader').hide();
                jQuery('#check-out-date').show();
                jQuery('#check-out-date').append(data);
            }});
            return false;
        });
        jQuery('#check-out-date').change(function(){
            alert(jQuery('#check-out-date').val());
        });
    });

This is what I wrote on function.php

Read More

Note: this should work in post type called “meetings”

add_action("wp_enqueue_scripts", function() {

     if (is_single()) {
        if (get_post_type() == 'meetings')
        {
            wp_enqueue_script('combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array( 'jquery' ), '1.0' ,true);

            $data_array = array(
                'ajaxurl' => admin_url( 'admin-ajax.php' )
            );

            wp_register_script( 'combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array('jquery') );
            wp_localize_script( 'combo_checkout_iRange', 'myAjax', $data_array );



        }
    }
});

and this is my ajax handler i put it inside single_meetings.php

add_action("wp_ajax_my_ajax_handler", "my_ajax_handler");
add_action("wp_ajax_nopriv_my_ajax_handler", "my_ajax_handler");

function my_ajax_handler() {
                if ( isset($_REQUEST["parent_id"]) ) {
                     $id    = $_REQUEST["parent_id"];
                     return $id;
                     die();
                 }
             }

Related posts

Leave a Reply

1 comment

  1. You can’t use return in your AJAX callback. The code never gets to die on the line beneath. You need to echo the value instead and then use wp_die().

    Replace:

    return $id;
    die();
    

    With:

    echo $id;
    wp_die();