javascript - The WordPress Ajax request returns 0 -
i can't figure out why nothing returned real beginner in ajax .. read lot of topics using ajax in woprdpress examples super advanced me here 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 wrote on function.php
note: 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 ajax handler put 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(); } }
you can't use return
in ajax callback. code never gets die
on line beneath. need echo value instead , use wp_die()
.
replace:
return $id; die();
with:
echo $id; wp_die();
Comments
Post a Comment