请在将此问题标记为重复之前阅读此内容。
我的错误是使用header命令,它不允许我的表单重定向它在提交后启动表单并且不会将我发送到页面。我不是一个非常好的程序员,我现在很迷茫。
我的背景是设计,由于我的解释不足,我的上一个问题被标记为重复。请理解,任何错误的沟通都不是故意的,我已经在这个网站上阅读所有其他问题几个小时而没有到达任何地方,所以任何有关这方面的帮助都可以用更简单的术语表达。
我在下面添加了我的代码,它包含header()命令以及我如何合并它。 Dreamweaver不显示任何代码错误,但页面在正确提交后拒绝重定向到URL。
<!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(
"info@email.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>
&#13;
答案 0 :(得分:2)
在将响应发送到客户端后,您无法发送标头。在您的页面上的任何内容之前移动您的PHP代码(基本上在doctype声明之前的情况下)。
摘自关于header()的官方PHP手册:
请记住,在任何实际输出之前必须调用header() 通过普通HTML标记,文件中的空行或PHP发送。 使用include或require读取代码是一个非常常见的错误, 函数或其他文件访问函数,并且有空格或空 调用header()之前输出的行。一样的问题 使用单个PHP / HTML文件时存在。
答案 1 :(得分:0)
如果你想使用header('Location:...')
,那么它必须是你发送给浏览器的第一件事,如果发送任何东西到浏览器你的重定向将无法工作。这包括任何http标头。
请移动您的代码,将发送重定向的电子邮件发送到您脚本的最顶层。
为确保在重定向之前不向浏览器发送任何内容(包括标题),请将ob_start()
添加到脚本的开头,然后ob_end_flush()
添加到结尾。这将打开输出缓冲,这意味着所有输出都将被缓冲,并且不会发送到浏览器,直到它到达您的重定向或ob_end_flush
。您需要确保在重定向之前清除缓冲区并在重定向后退出脚本。
<?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(
"info@email.com",
"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(); ?>