I have been trying to get my PHP contact form to go to a different page after it is properly submitted. Currently this form works great as a means to send an email to my account and inform the user it has not been properly filled out, adding a redirect has not worked well though.
Doing research i have heard many people mention the header() command and how it works for redirects but have been unsuccessful in getting it to work with my code. I have updated the code to represent how i used the header command. Currently all it does is send my page back to itself and starts the form over. Dreamweaver does not show any errors in code
Any help would be appreciated. I have placed my code below
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="shortcut icon" href="startup/flat-ui/images/favicon.ico">
<link rel="stylesheet" href="startup/flat-ui/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="startup/flat-ui/css/flat-ui.css">
<!-- Using only with Flat-UI (free)-->
<link rel="stylesheet" href="startup/common-files/css/icon-font.css">
<!-- end -->
<link rel="stylesheet" href="startup/common-files/css/animations.css">
<link rel="stylesheet" href="static/css/style.css">
<title></title>
</head>
<body>
<div class="page-wrapper">
<section id="contactform" class="contacts-1">
<div class="container">
<div class="row">
<div class="col-sm-8">
<h3>Step 1: Information Form</h3>
</div>
</div>
<div class="row">
<div class="col-sm-8">
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
// Form variables
// req: name, ship, ctname, ctnum, email
$goto_after_mail = "http://www.recycleoilsep.com/step2.html";
$formName = filter_var($_POST['formName'], FILTER_SANITIZE_STRING);
$formShip = filter_var($_POST['formShip'], FILTER_SANITIZE_STRING);
$formCity = filter_var($_POST['formCity'], FILTER_SANITIZE_STRING);
$formState = filter_var($_POST['formState'], FILTER_SANITIZE_STRING);
$formZip = filter_var($_POST['formZip'], FILTER_SANITIZE_STRING);
$formCtname = filter_var($_POST['formCtname'], FILTER_SANITIZE_STRING);
$formCtnum = filter_var($_POST['formCtnum'], FILTER_SANITIZE_STRING);
$formEmail = filter_var($_POST['formEmail'], FILTER_SANITIZE_STRING);
// CAPTCHA
require_once('captcha/recaptchalib.php');
$privatekey = "";
$resp = recaptcha_check_answer(
$privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]
);
// Form validation
$nameError = $formName == "";
$shipError = $formShip == "";
$cityError = $formCity == "";
$stateError = $formState == "";
$zipError = $formZip == "";
$ctnameError = $formCtname == "";
$ctnumError = $formCtnum == "";
$emailError = $formEmail == "";
$captchaError = !($resp->is_valid);
$hasError = $nameError || $shipError || $cityError || $stateError || $zipError || $ctnameError || $ctnumError || $emailError ;
if(!$hasError && !$captchaError) {
$headers = 'From: '.$formName.' <'.$formEmail.'>' . "\r\n";
$headers .= 'Reply-To: ' . $formEmail . "\r\n";
$headers .= 'Return-Path: ' . $formEmail . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$body =
"
<p><b>Company Name: </b>$formName</p>
<p><b>Pickup Address: </b>$formShip</p>
<p><b>Pickup City: </b>$formCity</p>
<p><b>Pickup State: </b>$formState</p>
<p><b>Pickup Zip: </b>$formZip</p>
<p><b>Contact Name: </b>$formCtname</p>
<p><b>Contact Phone: </b>$formCtnum</p>
<p><b>Contact Email: </b>$formEmail</p><br>
";
mail(
"info@website.com",
"A new request from website.com!",
$body,
$headers
);
header("location: ".$goto_after_mail);
} else {
echo "<p>There was an error submitting the form. Please check all the marked fields.</p>";
if ($captchaError) {
echo "<p>Captcha error. Please type the checkwords again.</p>";
}
}
}
?>
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'clean'
};
</script>
<div class="col-sm-12">
<form id="contactForm" action="" method="post">
<div class="control-group<?php if ($nameError) echo " error"; ?>">
<label class="control-label" for="formName"><strong>Company Name</strong></label>
<div class="controls">
<input type="text" name="formName" value="<?php echo $formName; ?>">
</div>
</div>
<div class="control-group<?php if ($shipError) echo " error"; ?>">
<label class="control-label" for="formShip"><strong>Address for Pickup</strong></label>
<div class="controls">
<input type="text" name="formShip" value="<?php echo $formShip; ?>">
</div>
</div>
<div class="col-sm-4">
<div class="control-group<?php if ($cityError) echo " error"; ?>">
<label class="control-label" for="formCity"><strong>City</strong></label>
<div class="controls">
<input type="text" name="formCity" value="<?php echo $formCity; ?>">
</div>
</div>
</div>
<div class="col-sm-4">
<div class="control-group<?php if ($stateError) echo " error"; ?>">
<label class="control-label" for="formState"><strong>State</strong></label>
<div class="controls">
<input type="text" name="formState" value="<?php echo $formState; ?>">
</div>
</div>
</div>
<div class="col-sm-4">
<div class="control-group<?php if ($zipError) echo " error"; ?>">
<label class="control-label" for="formZip"><strong>Zip</strong></label>
<div class="controls">
<input type="text" name="formZip" value="<?php echo $formZip; ?>">
</div>
</div>
</div>
<div class="control-group<?php if ($ctnameError) echo " error"; ?>">
<label class="control-label" for="formCtname"><strong>Contact Name</strong></label>
<div class="controls">
<input type="text" name="formCtname" value="<?php echo $formCtname; ?>">
</div>
</div>
<div class="control-group<?php if ($ctnumError) echo " error"; ?>">
<label class="control-label" for="formCtnum"><strong>Contact Number</strong></label>
<div class="controls">
<input type="text" name="formCtnum" value="<?php echo $formCtnum; ?>">
</div>
</div>
<div class="control-group<?php if ($emailError) echo " error"; ?>">
<label class="control-label" for="formEmail"><strong>Contact Email</strong></label>
<div class="controls">
<input type="email" name="formEmail" value="<?php echo $formEmail; ?>">
</div>
</div>
<br>
<div class="control-group">
<div class="controls">
<?php
// load recaptcha file
require_once('captcha/recaptchalib.php');
// enter your public key
$publickey = "-";
// display recaptcha test fields
echo recaptcha_get_html($publickey);
?>
</div>
</div>
<div class="control-group">
<div class="controls">
<br />
<input type="submit" class="btn btn-info" value="Go To Step 2" tabindex="5" id="submit" name="submit">
<a href="step2.html">Click Here to go to Step 2</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</section>