2015-01-15 106 views
0

我打電話給PHP函數發送一封包含表單信息的電子郵件,電子郵件正在成功發送和接收,當我調試PHP函數時,它返回TRUE。問題在於AJAX調用正在捕獲[object Object]錯誤,並在AJAX調用中的錯誤部分中觸發該函數,而不是成功調用該函數。有誰知道這可能是什麼原因造成的?Ajax調用PHP成功觸發錯誤

這裏是我的Java Script代碼:

<script type="text/javascript"> 
$("#candidate_form").submit(function(){ 


$.ajax({ 
    type: "POST", 
    url: "php/mail.php", 
    data: { fname: $("#fname").val(), lname: $("#lname").val(), email: $("#email").val(), phone: $("#phone").val()}, 
    dataType: "json", 
    success: function(response){ 
     alert(response) 
     $("#jobModal").show(); 
    }, 
    error: function(response){ 
     alert("This action failed with the following error" + response); 
    } 
    }) 
    .done(function(data) { 
    $("#jobModal").modal("show"); 
    }) 


}); 

</script> 

這是我的PHP代碼:

所有的
<?php 


if (isset($_POST)) { 
sendCandidate(); 
} 

function sendCandidate() { 

$emailTo="[email protected]"; 
$subject="Candidate Info Submited from Website"; 
$body= "Hello! 

A new candidate has submited his/her information from our website. Please check the candidate´s  information and contact him/her as soon as possible. 

Name: ". $_POST['fname']. " ". $_POST['lname']. 
" 
Email: ".$_POST['email']. 
" 
Phone: ". $_POST['phone']. 
" 

Regards! 

-- 
Jobs 
"; 
$headers="From: [email protected]"; 

#Sending mail with candidate´s info to recruiters. 
$candidateMailSent = mail($emailTo, $subject, $body, $headers); 


#Sending thank you email to candidate. 
mail($_POST['email'], "Jobs - Resume submited", 
"Hello ". $_POST['fname']." ". $_POST['lname']. ","." 

We really appreciate your interest in working with us towards getting your new job. We have received your Resume and Contact Information and one of our Specialized Recruiters  will be getting in touch with you as soon as possible. 

If you have any doubts please feel free to contact us by sending an email to  [email protected] with the Subject: Candidate Inquiry. 

Regards! 

-- 
Jobs 


", $headers); 





return json_encode($candidateMailSent); 
} 



?> 

回答

0

首先,當你做一個json_encode,它需要一個陣列上進行。 Mail會返回一個布爾值,您將其分配給$candidateMailSent。你應該把它分配給$candidateSentMail['success']

在php文件中它表示返回值是true或false。我沒有看到它回覆了一條消息。

然後在jQuery的方面,你將考驗響應,responce.success

前,

success: function(response.success){ 
     alert(response.success) 
     $("#jobModal").show(); 
    }, 
    error: function(response.success){ 
     alert("This action failed with the following error" + response.success); 
    }