成功提交时重定向php表单

时间:2011-03-06 13:00:27

标签: php redirect

在提交表单后,使用以下代码:

<?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>";}
?>

如何在提交时重定向到谢谢页面(如果当然没有错误)?

表单操作目前是:

<form action="<?php echo $_SERVER['../PHP_SELF']; ?>" method="post" id="enquiryform">

我在执行sql查询后尝试了以下操作。我想仅在成功提交表单(没有任何错误)时将此页面重定向到感谢页面

header('Location: THANK YOU PAGE');

我确信这是相当明显的事情,但我到处搜寻并尝试了很多不同的事情无济于事!

完整代码(显然删除了服务器信息,电子邮件地址和表单内容等等):

<?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@EMAILADDRESS.COM';
$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>  

2 个答案:

答案 0 :(得分:3)

你可以设置这样的功能:

function pageRedirect ($page) {
    if (!@header("Location: ".$page))
       echo "\n<script type=\"text/javascript\">window.location.replace('$page');</script>\n";
    exit;
}

然后在你的代码中使用它:

<?php
if(isset($error))
    echo "<span id='Warning'>Please enter all areas marked with *</span>";
else 
    pageRedirect ($thankyoupage);
?>

然后,您可以将所有愚蠢的蚂蚁放在谢谢的页面中

答案 1 :(得分:0)

添加

exit(); 
标题调用后

<?php
if(isset($error)) {
    echo "<span id='Warning'>Please enter all areas marked with *</span>";
}
else {
    header("Location : thank you page");
    exit();
}
?>