我有几个使用相同的通用 AJAX 代码的联系表格:
$(document).ready(function() {
$('#mainMail').submit( function() {
$.ajax({
type: "POST",
url : "<?php echo home_url(); ?>/wp-content/themes/retlehs-roots-f45f4f5/assets/bin/process.php",
data: $(this).serialize(),
success: function() {
$('#enquiryBox #submitSuccess').removeClass('hidden');
setTimeout(function() { $("#enquiryBox #submitSuccess").addClass('hidden'); }, 15000);
},
error: function() {
$('#enquiryBox #submitFail').removeClass('hidden');
setTimeout(function() { $("#enquiryBox #submitFail").addClass('hidden'); }, 15000);
}
});
return false;
});
});
简而言之,当它成功提交表单并向我发送电子邮件(通过 process.php)时,#submitSuccess
div 会按应有的方式显示出来。问题是,当它失败时,div也会出现(#submitFail
div 保持隐藏状态)。
我猜这是因为只有在没有成功调用 process.php 时才会发生 ajax 发布失败?有没有办法调整它,以便如果电子邮件不是通过 process.php 发送给我,用户会收到错误消息?
这是我的 process.php 代码:
<?php
$robotest = $_POST['robotest']; //just testin' for robots
$recipient = "info@mydomain.com"; //recipient
$email = ($_POST['email']); //senders e-mail adress
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) {
$Name = ($_POST['name']); //senders name
$mail_body = "Hello, \r\n \r\n";
$mail_body .= "You have received a new booking with the following details: \r\n\r\n";
$mail_body .= "{$_POST['comments']}; \r\n \r\n";
$mail_body .= "Please respond to the customer within 30 minutes on the following phone number: {$_POST['phone']} \r\n";
$mail_body .= "Warm regards, \r\n";
$mail_body .= "Robot. \r\n";
$subject = "Free Consult Inquiry"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command :)
} else {
print "You've entered an invalid email address!";
}
?>
(这print "you've entered an invalid email address!"
是我对错误消息的尝试,但这也不起作用......)