2015-06-30 46 views
1

我在PHP中編寫郵件發件人,並通過jQuery與jQuery調用它。這是我的PHP代碼的一部分:發送郵件後PHP/jQuery AJAX錯誤響應

// Send the email. 
     if (mail($recipient, $subject, $email_content, $email_headers)) { 
      // Set a 200 (okay) response code. 
      http_response_code(200); 
      echo "Thank You! Your message has been sent."; 
     } else { 
      // Set a 500 (internal server error) response code. 
      http_response_code(500); 
      echo "Oops! Something went wrong and we couldn't send your message."; 
     } 

這是我的JS:

$(form).submit(function(e) { 
     // Stop the browser from submitting the form. 
     e.preventDefault(); 

     // Serialize the form data. 
     var formData = $(form).serialize(); 

     // Submit the form using AJAX. 
     $.ajax({ 
      type: 'POST', 
      url: $(form).attr('action'), 
      data: formData 

     }) 
     .done(function(response) { 
      // Make sure that the formMessages div has the 'success' class. 
      $(formMessages).removeClass('error'); 
      $(formMessages).addClass('success'); 

      // Set the message text. 
      $(formMessages).text(response); 

      // Clear the form. 
      $('#nombre').val(''); 
      $('#email').val(''); 
      $('#telefono').val(''); 
      $('#pais').val(''); 
     }) 
     .fail(function(data) { 
      // Make sure that the formMessages div has the 'error' class. 
      $(formMessages).removeClass('success'); 
      $(formMessages).addClass('success'); 

      // Set the message text. 
      if (data.responseText !== '') { 
       $(formMessages).text(data.responseText); 
      } else { 
       $(formMessages).text('Oops! An error occured and your message could not be sent.'); 
      } 
     }); 
    }); 

和HTML表單,以防萬一:

<form id="ajax-contact" method="post" action="mailer.php"> 
    <ul> 
     <li><input name="nombre" id="nombre" class="inputNombre" placeholder="Nombre" required/></li> 
     <li><input name="email" id="email" placeholder="E mail" type="email" class="inputEmail" required/></li> 
     <li><input name="telefono" placeholder="Teléfono" class="inputTelefono" id="telefono" required/></li> 
     <li><input name="pais" placeholder="País" class="inputPais" id="pais" required /></li> 
     <li><input name="submit" type="submit" value="Enviar" class="botonEnviar" id="submit"></input></li> 
    </ul> 
</form> 

這裏奇怪的是,即使發送郵件,這總是會轉到else分支並設置500代碼,所以當我試圖管理響應時,我得到了錯誤的答案。但我收到郵件,所以if正在工作。

我總是得到響應代碼500,據說設置爲郵件失敗時。

我在做什麼錯?

+0

您標記爲AJAX,但您沒有任何JavaScript代碼。您應該添加更多的代碼,因爲您發佈的代碼片段是有效的。 –

+0

我添加了更多的代碼來澄清,對不起。 – Ikzer

+0

檢查PHP錯誤日誌。 –

回答

0

嘗試增加elseif(!)

if (mail($recipient, $subject, $email_content, $email_headers)) { 
       // Set a 200 (okay) response code. 
       http_response_code(200); 
       echo "Thank You! Your message has been sent."; 
      } 

elseif (!mail($recipient, $subject, $email_content, $email_headers)) { 
       // Set a 500 (internal server error) response code. 
       http_response_code(500); 
       echo "Oops! Something went wrong and we couldn't send your message."; 
      } 
+0

不,我得到的代碼500,但郵件發送。 – Ikzer

0

檢查您的網絡託管的PHP版本。您只能從PHP 5.4設置response codes。編輯:我今天遇到了完全相同的問題。郵件出去了,PHP返回了500響應。我試圖在PHP 5.5.21上運行另一臺服務器上的代碼,它的工作完美無瑕。