php php表单提交后如何重定向到成功/失败页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16818279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:43:54  来源:igfitidea点击:

How to redirect to success / fail pages after php form submission?

php

提问by Chris

I have successfully integrated the following form submission code into my site, and it works great. However I would like to have the code redirect the user to one page if the form submission is successful, and a different page if it fails. How could I adapt the following code to do that? It's really starting to get on my nerves! :-P

我已成功将以下表单提交代码集成到我的网站中,并且效果很好。但是,如果表单提交成功,我希望代码将用户重定向到一个页面,如果提交失败,则将用户重定向到另一个页面。我怎样才能修改以下代码来做到这一点?我真的开始紧张了!:-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 protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

EDIT:

编辑:

Ok I have changed the code to:

好的,我已将代码更改为:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enquiry = $_POST['enquiry'];
$formcontent=" From: $name \n Phone: $phone \n Message: $enquiry";
$recipient = "[email protected]";
$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;
?>

This works however it never goes to the fail page. I'm guessing that's because the email is always sent even if fields are empty. I have jquery verification in place (which I have disabled for testing purposes) but that obviously only works for users with javascript enabled. How could I alter the code to only show the success page if the form fields contain data? Any help is appreciated.

这有效,但是它永远不会进入失败页面。我猜这是因为即使字段为空,也会始终发送电子邮件。我有 jquery 验证(为了测试目的我已经禁用了它),但这显然只适用于启用了 javascript 的用户。如果表单字段包含数据,我如何更改代码以仅显示成功页面?任何帮助表示赞赏。

回答by Get Off My Lawn

add a redirect to the page:

添加重定向到页面:

// Test to see if variables are empty:
if(!empty($name) && !empty($email) && !empty($phone) && !empty($enquiry)){
    // Test to see if the mail sends successfully:
    if(mail($recipient, $subject, $formcontent, $mailheader)){
        header("Location: success.php");
    }else{
        header("Location: error.php");
    }
}else{
    header("Location: back_to_form.php");
}

回答by Petr ?ihula

mail function returns true/false when succeeds/fails. So it's rather simple:

邮件函数在成功/失败时返回真/假。所以它相当简单:

if (mail($recipient, $subject, $formcontent, $mailheader)) {
    header('location: success.php');
} else {
    header('location: fail.php');
}

回答by Jared Eitnier

Add

添加

header("Location: successpage.html");

to the bottom of your code and remove echo "Thank you!";

到代码底部并删除 echo "Thank you!";

回答by K Boden

Rather than send the client a redirect, resulting in another call to your web server, I believe a better way of doing this would be to use a PHP include.

而不是向客户端发送重定向,导致另一个调用您的 Web 服务器,我相信这样做的更好方法是使用 PHP 包含。

if (mail($recipient, $subject, $formcontent, $mailheader))
    include 'success.php';
else
    include 'fail.php';

回答by Ryan

Get rid of the echo and then use a header:

摆脱回声,然后使用标题:

header("Location: success.php");

If it fails, redirect to error.php

如果失败,重定向到 error.php

header("Location: error.php");

If you want to go to the page the form was on, but show an error or success message, do this:

如果您想转到表单所在的页面,但显示错误或成功消息,请执行以下操作:

header("Location: original.php?status=error")

Or change error to success if appropriate, you can then use $_GET['status']to determine whether or not the form failed / succeeded.

或者在适当的情况下将错误更改为成功,然后您可以使用它$_GET['status']来确定表单是否失败/成功。

回答by Ziarno

if (failure) {
  header("Location: success.php");
  exit;
} else if (success) {
  echo "thank you";
}