我正在尝试在wordpress中使用ajax
我有两个文件,php和js
这是我的PHP代码
combo_check-out.php
function iRange($first, $last, $format = 'm/d/Y' ) {
$dates = array();
$current = strtotime($first);
$i=1;
while( $i <= $last ) {
$dates[] = date($format, $current);
$current = strtotime('+1 day', $current);
$i++;
}
$time = date("m/d/Y",$current);
return $time;
}
if($_REQUEST)
{
$id = $_REQUEST['parent_id'];
?>
<select name="check-out" id="check-out-date">
<option value="<?php echo iRange($id, 1, $format = 'm/d/Y' ) ?>">"1 Day (Same Day)"</option>
<option value="<?php echo iRange($id, 2, $format = 'm/d/Y' ) ?>">"2 Days"</option>
<option value="<?php echo iRange($id, 3, …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么没有返回我在Ajax中是一个真正的初学者..我刚刚阅读了很多关于在Woprdpress中使用Ajax的主题但是这些例子对我来说非常先进这里是我的JS代码 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());
});
});
Run Code Online (Sandbox Code Playgroud)
这就是我写的 function.php
注意:这应该适用于称为"会议"的帖子类型
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 ); …Run Code Online (Sandbox Code Playgroud)