我試圖使用jQuery AJAX和PHP來創建聯繫表單。我的HTML如下:AJAX聯繫表格的PHP代碼
<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 = '[email protected]'; //my email address here
$subject = 'Message From :' . $_POST['name'];
$message = $_POST['message'];
$headers = 'From :' . $_POST['email'].'\r\n';
mail($to,$subject,$message,$headers);
}
?>
但它不起作用。成功消息顯示,但沒有收到郵件。 我對PHP很不熟悉。
請幫助....
可以嘗試發送通過爲您的php部分輸入手動值來發送電子郵件?我只想檢查一下你的郵件機制是否運行良好。 – Sina
我試過了,但沒有收到郵件 –
的郵件地址?從你的本地主機或現場服務器? –