[英]No email sent with Ajax contact form (in Wordpress)
I'm trying to build a simple Ajax contact form in Wordpress, with validation (jQuery Validate plugin) with this code: 我正在尝试在Wordpress中使用以下代码通过验证(jQuery Validate插件)构建一个简单的Ajax联系人表单:
Html HTML
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
Js s
$("#form").validate({ //jquery validate plugin
... // rules
submitHandler: function(form) {
var data = $('form').serialize();
$.ajax({
url: ajaxurl,
type: "POST",
data: data,
cache: false,
action: 'sendmail',
success: function (html) {
... // succesful message
}
});
return false;
} // end submitHandler
}); // end Validate
function.php function.php
function send_my_mail(){
... //sends email
}
add_action('wp_ajax_sendmail', 'send_my_mail');
add_action('wp_ajax_nopriv_sendmail', 'send_my_mail');
When I submit the form the browser console retrieves this ok message: 当我提交表单时,浏览器控制台会检索以下确定消息:
POST http://www.domain.com/wp-admin/admin-ajax.php 200 OK 3.45s
but no email was sent. 但没有发送电子邮件。 I don't get what's wrong in the code!
我没有明白代码有什么问题!
The problem is that you use an "action" option that doesnt exists in jQuery.ajax. 问题是您使用的是jQuery.ajax中不存在的“操作”选项。 You just need to pass the action to WP in the data option like this :
您只需要将操作通过data选项传递给WP,如下所示:
$("#form").validate({ //jquery validate plugin
... // rules
submitHandler: function(form) {
var data = $('form').serialize();
$.ajax({
url: ajaxurl,
type: "POST",
data: data + '&action=sendmail',
cache: false,
success: function (html) {
... // succesful message
}
});
return false;
} // end submitHandler
}); // end Validate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.