我正在通過ajax發送一封電子郵件以聯繫php。 PHP腳本成功發送電子郵件,但阿賈克斯xmlhttp.readyState是保持returing 2即使xmlhttp.status是200xmlhttp.readyState返回2,即使xmlhttp.status等於200
params = "name=" + name + "&email=" + email + "&message=" + message + "&telephone=" + telephone;
xmlhttp.open("POST", "contact.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
if(xmlhttp.responseText == "fill_form"){
note.innerHTML = "Please fill the required fields properly";
return;
}
if(xmlhttp.responseText == "Sent"){
serverMessage.innerHTML = "Thanks. If it is a request or complaint we well get back to you soon";
}
}
else{
serverMessage.innerHTML = "Some internal error occured while sending the email. Please try again later";
$('#myModal').modal('show')
}
submitBtn.innerHTML = "SEND MESSAGE";
submitBtn.disabled = false;
}
Contact.php
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$telephone=$_POST['telephone'];
$mail_to_send_to = "[email protected]";
$feed_back_mail = "[email protected]";
if (empty($name) || empty($email)|| empty($message))
{
echo "fill_form";
}
else{
$from="From:$feed_back_mail"."\r\n"."Reply-To:$email"."\r\n" ;
$subject="Users feed back Contact";
if(empty($telephone)){
$telephone = "No telephone sent my user";
}
$message = "Telephone: $telephone\r\nSender's Email : $email \r\n \r\n$message \r\n";
$isSent = mail($mail_to_send_to, $subject, $message, $from);
if($isSent){
echo $isSent;
}
else{
echo "not_sent";
}
}
?>
什麼我做錯誤?
那麼,爲什麼不xmlhttp.readyState是4? – ash
請檢查我的更新答案! –