为什么AJAX响应为0!让我很恼火!。我想在blur上检查电子邮件地址是否有效。请建议我这是我的代码这是我的php代码
add_action(\'wp_ajax_send_message_to_customer\',\'add_send_msg\');?>
<form>
<input type="text" name="email_customer" id="email_customer"/>
</form>
<div id="email_info"></div>
<?php
function add_send_msg(){
$email = isset($_POST[\'email_customer\']) ? $_POST[\'email_customer\'] : null;
if( $email ){
if(is_email($email)){
$msg = \'valid\';
}
}
echo $msg;
die();
}
添加js和本地化脚本 add_action( \'init\', \'my_script_enqueuer\' );
function my_script_enqueuer() {
wp_enqueue_script( "send_email", path_join(WP_PLUGIN_URL,basename(dirname(__FILE__)).\'/send_email.js\', array(\'jquery\')));
wp_localize_script( \'send_email\', \'myAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' )));
}
和js文件发送电子邮件。js公司jQuery(document).ready(function () {
jQuery("input[name=\'email_customer\']").blur(function (){
dataString = "email_customer="+jQuery(this).attr("value")+"&action=send_message_to_customer";
jQuery.ajax({
type: "POST",
data: "email_customer="+jQuery(this).attr("value")+"&action=send_message_to_customer",
url: myAjax.ajaxurl,
beforeSend: function(){
jQuery("#email_info").html("Checking Email......");
},
success: function(data) {
alert(data);
if( data == \'valid\'){
jQuery("#email_info").html("Email Ok");
}
else{
jQuery("#email_info").html("You have entered invalid email");
}
}
});
});
});