Reputation: 11
Please read this before your mark this question as duplicate.
My Error is with the header command, it is not allowing my form to redirect it starts the form over after submission and does not send me to the page. I am not a very good coder and i am very lost right now.
My background is in design and my last question was flagged as duplicate due to my lack of explanation. Please understand that any mis-communication is not on purpose and i have been on this site for hours reading all the other questions and not getting anywhere, so any help on this that could be put in more simple terms would be appreciated.
I have added my code below and it DOES contain the header() command and how i incorporated it. Dreamweaver does not show any code errors but the page refuses to redirect to the URL after proper submission.
<!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.website.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(
"[email protected]",
"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>
Upvotes: 1
Views: 58
Reputation: 340
If you want to use header('Location:...')
then it needs to be the first thing you send to the browser, if anything at all is sent to the browser your redirect won't work. That includes any http headers.
So move your code for sending the email issuing the redirect to the very top of your script.
To be sure you send nothing to the browser before your redirect, including headers, add ob_start()
to the beginning of your script and ob_end_flush()
to the end. This will turn on output buffering, meaning all output will be buffered and not sent to the browser until either it hits your redirect or the ob_end_flush
. You need to make sure you clear the buffer before the redirect and exit the script after the redirect.
<?php
ob_start();
if($_SERVER['REQUEST_METHOD'] == "POST") {
// Form variables
// req: name, ship, ctname, ctnum, email
$goto_after_mail = "http://www.website.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(
"[email protected]",
"A new request from website.com!",
$body,
$headers
);
ob_end_clean();
header("location: ".$goto_after_mail);
exit;
} 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>";
}
}
}
?>
<!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">
<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>
<?php ob_end_flush(); ?>
Upvotes: 0
Reputation: 2405
You cannot send headers after response has been sent to the client. Move your php code before any content on your page (basically in your case before doctype declaration).
Excerpt from official PHP manual regarding header():
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Upvotes: 2