php 成功提交时重定向php表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5210624/
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
Redirect php form on successful submit
提问by Motley9735
At the moment after a form is submitted, the following code is used:
在提交表单后的那一刻,使用以下代码:
<?php
if(isset($error))
{echo "<span id='Warning'>Please enter all areas marked with *</span>";}
else if (isset($sent))
{echo "<span id='Normal'>Thank you for your enquiry, we will contact you shortly</span>";}
?>
How can I redirect to a THANK YOU page upon submit (if there are no errors of course)?
如何在提交时重定向到感谢页面(如果当然没有错误)?
The form action is currently:
表单操作目前是:
<form action="<?php echo $_SERVER['../PHP_SELF']; ?>" method="post" id="enquiryform">
I've tried the following after my sql query execution. I would like to redirect this page to a thank you page only on successful form submission (without any errors)
我在执行 sql 查询后尝试了以下操作。我想仅在成功提交表单时将此页面重定向到感谢页面(没有任何错误)
header('Location: THANK YOU PAGE');
I'm sure it's something fairly obvious, but I've searched everywhere and tried lots of different things to no avail!
我确定这是相当明显的事情,但我到处搜索并尝试了很多不同的东西,但无济于事!
Full code (obviously removing server info, email addresses and form content as it's fairly long etc):
完整代码(显然删除了服务器信息、电子邮件地址和表单内容,因为它相当长等):
<?php
/* Accessing SQL-Server and querying table */
MYSQL_CONNECT($server, $user, $password) or die ("Server unreachable");
MYSQL_SELECT_DB($database) or die ("Database non existent");
if(array_key_exists('submit',$_POST))
{
$adate = $_POST['adate'];
$guests = $_POST['guests'];
if($guests=="Please Select")
{
$error['guests'] = 'Flagged';
}
$title = $_POST['title'];
if($title=="Please Select")
{
$error['title'] = 'Flagged';
}
$name = trim($_POST['fullname']);
if(empty($name))
{
$error['name'] = 'Flagged';
}
$telephone = trim($_POST['telnumber']);
if(empty($telephone))
{
$error['telephone'] = 'Flagged';
}
$accomm = trim($_POST['accommodation']);
if($accomm=="Default")
{
$error['accommodation'] = 'Flagged';
}
$email = $_POST['email'];
$pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!preg_match($pattern, trim($email)))
{
$error['email'] = 'Flagged';
}
$message = trim($_POST['message']);
//initialize variables
$to = '[email protected]';
$subject = "Enquiry";
//build the message
$email_message .= 'Arrival Date: '.$adate.' Guests: '.$guests.' Accom: '.$accomm."\n\n";
$email_message .= 'Name: '.$title.' '.$name."\n\n";
$email_message .= 'Telephone: '.$telephone."\n\n";
$email_message .= 'Email: '.$email."\n\n";
$email_message .= 'Message: '.$message;
$additionalHeaders = "From: XXXXXXXXXXXX<".$email.">";
//print_r($error);
//send the email
if (!isset($error))
{
mail($to, $subject, $email_message, $additionalHeaders);
MYSQL_QUERY("INSERT into Enquiry VALUES('".date('d/m/y')."','".$_POST['hSpa']."','".$_POST['hPackage']."','".$adate."','".$guests."','".$accomm."','".$title."','".$name."','".$telephone."','".$email."','".$message."')");
}
}
?>
</head>
<body id="body">
<form action="<?php echo $_SERVER['../PHP_SELF']; ?>" method="post" id="enquiryform">
<p>Areas marked with * must be completed</p>
<label class="enquiryform" id="message" for="message">Message</label>
<textarea name="message" id="message" rows="9" cols="55" tabindex="9" onclick="removeDefaultText(this)"><?php if(isset($error)) {echo $message;} else {echo "Please use this area for any other information about your enquiry";}?></textarea>
<input type="submit" id="submit" name="submit" value="Send enquiry" tabindex="10" />
</form>
</div>
<?php
if(isset($error)) {
echo "<span id='Warning'>Please enter all areas marked with *</span>";
}
else if (isset($sent)) {
header("Location: THANK YOU PAGE.HTML");
exit();
}
?>
</div>
回答by Lucius
you could set a function like this:
你可以设置这样的函数:
function pageRedirect ($page) {
if (!@header("Location: ".$page))
echo "\n<script type=\"text/javascript\">window.location.replace('$page');</script>\n";
exit;
}
then use it in your code like this:
然后在您的代码中使用它,如下所示:
<?php
if(isset($error))
echo "<span id='Warning'>Please enter all areas marked with *</span>";
else
pageRedirect ($thankyoupage);
?>
You can then put all the thankyous ant the blahblah in the thankyou page
然后,您可以将所有感谢蚂蚁放在感谢页面中 blahblah
回答by Lenin Raj Rajasekaran
Add
添加
exit();
after the header call.
在标头调用之后。
<?php
if(isset($error)) {
echo "<span id='Warning'>Please enter all areas marked with *</span>";
}
else {
header("Location : thank you page");
exit();
}
?>