I usually put the error reporting and the redirect right after the form like this:
PHP Code:
</form>
<?php
if ($_POST && $errors) {
echo '<ul>';
foreach ($errors as $error) {
echo "<li>$error</li>";
}
echo '</ul>';
} elseif ($_POST && !$errors) {
echo "<script>window.location = 'thankYou.php'</script>";
}
?>
Above the HTML I setup things like e-mail validation and checking for duplicates in the database. I use code like this before the mailto even starts. This is from a contact form. All my contact forms use duplicate e-mail addresses for verification. The forms also always insert submission info into a table.
PHP Code:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$errors = array();
// remove leading and trailing spaces from input
$_POST = array_map('trim', $_POST);
// validate the email *** REQUIRES PHP 5.2 ***
if (!filter_input(INPUT_POST, 'eMail', FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Please enter a valid email address';
}
if ($_POST['eMail'] != $_POST['eMail2']) {
$errors[] = 'E-mail does not match';
}
// go ahead if no errors are detected
if (!$errors) {
// generate a unique token
$token = md5(uniqid(mt_rand(), true));
$timeNow = date(time());
$insertSQL = sprintf("INSERT INTO contact (firstName, lastName, eMail, timeNow, token) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['firstName'], "text"),
GetSQLValueString($_POST['lastName'], "text"),
GetSQLValueString($_POST['eMail'], "text"),
GetSQLValueString($timeNow, "text"),
GetSQLValueString($token, "text"));
mysql_select_db($database_civTekDB, $civTekDB);
$Result1 = mysql_query($insertSQL, $civTekDB);
$firstName = $_REQUEST['firstName'] ;
$lastName = $_REQUEST['lastName'] ;
$eMail = $_REQUEST['eMail'] ;
// generate an error message if the eMail is already in use
if (!$Result1 && mysql_errno() == 1062) {
$errors[] = $_POST['eMail'] . ' is already in use. Please use a different E-mail.';
} elseif (mysql_error()) {
$errors[] = 'Sorry, there was a problem with the database. Please try later.';
} else // ==== if all OK send an email to the user
// the rest of your send mail code follows . . .
This will give you clean error reporting right under the form and the redirect should always work when the submission is a success.
The only thing that I can think of that is breaking your code is that there is nothing to tell the redirect to work after the e-mail is sent. I don't see the problem but maybe this solution will work better for you.