简体   繁体   English

错误“无法修改标题信息-标题已经发送”。成功提交表单后如何重定向

[英]“Cannot modify header information - headers already sent by” error.How to redirect upon successful submission of the form

My code below uses header for redirection upon successful submission of the form and checks if all error variables are set to NULL for redirecting to another php page. 我下面的代码在成功提交表单时使用标头进行重定向,并检查是否将所有错误变量都设置为NULL以重定向到另一个php页面。 Please help me out as I have tried many ways but none seems to work for conditional redirection. 请尝试帮助我,因为我尝试了很多方法,但似乎没有一种方法可以用于条件重定向。

<?php
$proName=$proNameErr=$email=$emailErr=$catErr='';
if(isset($_POST["submit"]))
{
if (empty($_POST["proName"])) {
$proNameErr = "Product name is required";
}
else if(!preg_match('/^[a-z0-9 .&]+$/i',$_POST['proName'])){
$proNameErr = "Invalid product Name";
}

else{
$proName = test_input($_POST["proName"]);
}

if($_POST['cat']==0)
{
$catErr="please select an option";
}
else
{
$cat=$_POST['cat'];
}

if (empty($_POST["email"])) {
$email = "";
}
else{
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}

}
if($proNameErr=='' &&  $emailErr=='' && $catErr=='')
{
header("Location: addMedia.php", true, 302);
die();
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<!DOCTYPE html>
<html>
<body>
<h1>ADD Product</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"   method="post"  autocomplete="on">
Product Name: <input type="text" name="proName" placeholder="Product  Name" maxlength="35" size="40" required value="<?php echo $proName?>">*
<span class="error"><?php echo $proNameErr;?></span><br><br>

Category: <select name="cat" id="cat" size="1" required>
<option value=0>Select...</option>
<option value=1>John</option>
<option value=2>Paul</option>
<option value=3>Ringo</option>
<option value=4>George</option>
<option value=5>ram</option>
<option value=6>rahim</option>
</select>*<span class="error"><?php echo $catErr;?></span><br><br>

Email Address: <input type="email" name="email" value="<?php echo  $email?>"> <span class="error"><?php echo $emailErr;?></span><br><br>

<input type="submit" value="submit" name="submit">
</form>

Output buffering is your friend in this case. 在这种情况下,输出缓冲是您的朋友。 At the top of each page ( or at the very least, the page in question here ) add 在每个页面的顶部(或至少在这里有问题的页面)添加

ob_start();

/* the rest of the code */

/*and at the end*/

ob_end_flush();

You must have outputted something to the browser before header call. 标头调用之前,您必须已向浏览器输出了一些内容。 It cannot be exact copy of your code that you show us. 您向我们展示的代码不能完全相同。 Check: 校验:

  • is there any html before php opening tag 在php开头标记之前是否有任何html

  • is there any whitespace before php opening tag php开头标记前是否有空格

  • is another php script included with echo before that script 是该脚本之前echo中包含的另一个php脚本

  • is the coding utf8 with BOM? BOM编码的utf8是什么?

Those are possible reasons of your error. 这些可能是导致您出错的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法修改已发送的标头信息标头 - Cannot Modify Header Information Headers Already Sent 无法修改标题信息-标题已发送 - Cannot modify Header Information - headers already sent 错误:无法修改标头信息 - 已发送的标头 - Error: Cannot modify header information - headers already sent by 无法修改标头信息 - 标头已发送 - Cannot modify header information - headers already sent 无法修改标头信息-标头已由发送 - Cannot modify header information - headers already sent by 如何删除警告无法修改标头信息-标头已发送 - How to remove warning Cannot modify header information - headers already sent by 重定向到没有警告的页面:无法修改标题信息 - 已发送的标题 - redirect to a page without Warning: Cannot modify header information - headers already sent by 无法修改 header 信息 - 标头已发送 - PHP MVC:从一个 controller 重定向到另一个 Z5904C103F2C030 - Cannot modify header information - headers already sent - PHP MVC : Redirect from one controller to another controller Laravel 5.2 - phpunit - 无法修改标头信息 - 标头已发送 - Laravel 5.2 - phpunit - Cannot modify header information - headers already sent by 警告:无法修改标头信息-标头已发送 - Warning: Cannot modify header information - headers already sent by
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM