【发布时间】:2011-12-11 14:33:35
【问题描述】:
下午好,
我遇到了一个非常令人沮丧的问题。我目前正在尝试使用 AJAX 功能从我的 Wordpress 网站的联系表单中发送电子邮件。它只在没有填写任何字段时发送电子邮件,但是从我尝试在表单字段中提交数据的那一刻起,电子邮件就不会发送。
此外,由于某种原因,当我从 Wordpress 环境之外运行它时,它似乎可以工作,但是从我将它包含在 wordpress 模板文件中的那一刻起,问题就开始出现了。
下面是我的代码。请原谅目前没有任何错误或垃圾邮件处理的事实:
这是 Javascript:
$(document).ready(function(){
$('#emailform').submit(function(){
window.open('','',"width=500,height=150").document.write("Thank you for contacting us, we will reply you shortly. Thank you!");
// getting all the stuff from the form
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method'),
emailresponse = $('#emailresponse');
// loading stuff
emailresponse.fadeOut(200, function(){
$(this).text("Message sent")
.fadeIn(200)
});
// sending stuff to the server
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
success:function(data) {
//stuff to do when the ajax call is successful and complete
var responseData = $.parseJSON(data),
klass = '';
emailresponse.fadeOut(200, function(){
$(this).text(responseData.message)
.fadeIn(200);
});
}
})
return false;
})
})
这是php
if (isset($_GET['action'])) {
if($_GET['action'] == 'email') {
$name = $_POST['name'];
$email = mysql_real_escape_string($_POST['email']);
$message = $_POST['message'];
$m1 = "Name of customer: " . $name . "\n";
$m2 = "Customer's Email: " . $email . "\n";
$m3 = "Message: " . $message;
$emailmessage = $m1 . $m2 . $m3;
mail('seedorf@me.com', 'Message from LUNASKYMODA.CO.UK contact page', $emailmessage);
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
}
}
有问题的网页是http://lunaskymoda.co.uk/contact-us/
P.s,如果它说“消息已发送”并不一定意味着它已发送......仍在努力。
【问题讨论】:
-
这是 jQuery - 你在 标签中调用 jQuery 库吗?您在控制台中看到了什么错误?
标签: php jquery ajax forms wordpress