我试图创建一个使用jQuery AJAX和PHP的联系表单。我的HTML如下
<form action="#" id="contactForm" method="post" name="contactForm">
<fieldset>
<div class="col-sm-12">
<input id="contactName" name="contactName" placeholder="Your Name*" type="text" value="" required>
</div>
<!-- Name Field [ END ] -->
<div class="col-sm-12">
<input id="contactEmail" name="contactEmail" placeholder="Your Email*" type="email" value="" required>
</div>
<!-- Email Field [ END ] -->
<div class="col-xs-12">
<textarea cols="5" id="contactMessage" name="contactMessage" placeholder="Your Message....*" required></textarea>
</div>
<!-- Message Field [ END ] -->
<div class="col-xs-12">
<button class="submit">SEND</button>
</div>
<!-- Submit Button [ END ] -->
<div class="error col-xs-12">
<h3>Sorry! Your message was not sent.</h3>
</div>
<!-- Error Message [ END ] -->
<div class="success col-xs-12">
<h3>Success! Your message was sent.</h3>
</div>
<!-- Seccess Message [ END ] -->
</fieldset>
虽然jQuery代码如下
$('form#contactForm').submit(function () {
var url = $('#contactForm').attr('action'),
type = $('#contactForm').attr('method'),
contactName = $('#contactForm #name').val(),
contactEmail = $('#contactForm #email').val(),
contactMessage = $('#contactForm #message').val(),
data = {
name: contactName,
email: contactEmail,
message: contactMessage
};
$.ajax({
type: type,
url: url,
data: data,
success: function () {
$('.success').fadeIn();
$('.error').fadeOut();
},
error: function () {
$('.error').fadeIn();
$('.success').fadeOut();
}
});
return false;
});
什么可能的PHP代码使这个表单工作。我正在使用以下代码
<?php
if ( isset( $_POST['name'], $_POST['email'], $_POST['message'] ) ) {
$to = 'mail@example.com'; //my email address here
$subject = 'Message From :' . $_POST['name'];
$message = $_POST['message'];
$headers = 'From :' . $_POST['email'].'\r\n';
mail($to,$subject,$message,$headers);
}
?>
但它不起作用。成功消息显示但未收到邮件。 我不太熟悉PHP。
请帮助......
答案 0 :(得分:0)
您的成功消息显示因为php-page被成功调用,这并不表示邮件已成功发送。您应该通过返回邮件功能响应来检查:
$response = mail($to,$subject,$message,$headers);
echo $response;
这将返回true或false。此外,您还必须检查邮件是否正确接收,最终可能是垃圾邮件,还是由于各种原因被邮件服务器拒绝。如果是这种情况,您可以对邮件头进行一些添加。关于如何通过
获取邮件的Here are some pointers答案 1 :(得分:0)
尝试添加
method: "POST",
在$.ajax
来电中,默认情况下,$ .ajax使用GET请求
答案 2 :(得分:0)
这对你有用
$('form#contactForm').submit(function () {
var url = $('#contactForm').attr('action');
var type = $('#contactForm').attr('method');
var contactName = $('#contactName').val();
var contactEmail = $('#contactEmail').val();
var contactMessage = $('#contactMessage').val();
data = {
name: contactName,
email: contactEmail,
message: contactMessage
};console.log(data);
$.ajax({
type: type,
url: url,
data: data,
success: function () {
$('.success').fadeIn();
$('.error').fadeOut();
},
error: function () {
$('.error').fadeIn();
$('.success').fadeOut();
}
});
return false;
});
您的PHP代码变为
<?php
if ( isset( $_POST['name'], $_POST['email'], $_POST['message'] ) ) {
$to = $_POST['email']; //my email address here
$subject = 'Message From :' . $_POST['name'];
$message = $_POST['message'];
$headers = 'From :' . $_POST['email'].'\r\n';
mail($to,$subject,$message,$headers);
}
echo 'sucess';
?>
也在这里改变
<form action="===GIVE YOUR FILE NAME CONTAINING PHP mail() CONTAINS====" id="contactForm" method="post" name="contactForm">
您应确认您的服务器已启用mail()
功能
试试这个让我知道..