我已经成功地将以下表单提交代码集成到我的网站中,并且效果很好。但是,如果表单提交成功,我希望代码将用户重定向到一个页面,如果提交失败,则将用户重定向到另一个页面。我如何调整以下代码来做到这一点?真的开始让我紧张了!:-P
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name \n Phone: $phone \n Message: $enquiry";
$recipient = "email@email.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
编辑:
好的,我已将代码更改为:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name \n Phone: $phone \n Message: $enquiry";
$recipient = "email@email.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: mailer-success.htm");
}else{
header("Location: mailer-fail.htm");
}
exit;
?>
这有效,但是它永远不会进入失败页面。我猜这是因为即使字段为空,电子邮件也会始终发送。我有 jquery 验证(出于测试目的我已禁用),但这显然只适用于启用了 javascript 的用户。如果表单字段包含数据,我如何更改代码以仅显示成功页面?任何帮助表示赞赏。