Motley9735
Motley9735

Reputation: 105

Redirect php form on successful submit

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)

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>  

Upvotes: 2

Views: 21500

Answers (2)

Raj
Raj

Reputation: 22926

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();
}
?>

Upvotes: 0

Lucius
Lucius

Reputation: 963

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

Upvotes: 3

Related Questions