2015-08-21 90 views
1

我試圖使用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很不熟悉。

請幫助....

+0

可以嘗試發送通過爲您的php部分輸入手動值來發送電子郵件?我只想檢查一下你的郵件機制是否運行良好。 – Sina

+0

我試過了,但沒有收到郵件 –

+0

的郵件地址?從你的本地主機或現場服務器? –

回答

0

你sucess消息顯示becasue的PHP頁面成功調用,這並不表示該郵件成功發送。您應該通過返回郵件功能回覆來檢查:

$response = mail($to,$subject,$message,$headers); 
echo $response; 

這將返回true或false。此外,您還必須檢查郵件是否正確收到,最終可能是垃圾郵件,或者出於各種原因被郵件服務器拒絕。如果是這種情況,您可以對郵件標題做一些補充。 Here are some pointers如何通過

0

嘗試增加

method: "POST", 

在您的通話$.ajax,默認情況下$就使用GET請求

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()功能

啓用試試這個,讓我知道..