php 表单提交后重定向的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5182966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Right way to redirect after form submit
提问by iKaspars
Maybe its a stupid question, but what is right way to redirect user to the success page after form is saved in database?
也许这是一个愚蠢的问题,但是在将表单保存在数据库中后将用户重定向到成功页面的正确方法是什么?
I dont know why, but if I add action="done.php"
, than the form does not save data to my database.
我不知道为什么,但是如果我添加action="done.php"
, 比表单不会将数据保存到我的数据库中。
I tried to add header ("location:/done.php");
it worked, but when I moved page to original server ( PHP 4 and MySQL 3.23.5 ) there is error when I am trying to send the form Warning: Cannot modify header information - headers already sent by ........
我试图添加header ("location:/done.php");
它工作,但是当我将页面移动到原始服务器(PHP 4 和 MySQL 3.23.5)时,当我尝试发送表单时出现错误Warning: Cannot modify header information - headers already sent by ........
Here is my php code :
这是我的 php 代码:
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$adress = $_POST['adress'];
$post = $_POST['post'];
$phone = $_POST['phone'];
$sql="INSERT INTO tekstile_users (id, name, email, company, adress, post, phone)
VALUES
('', '$name','$email','$company', '$adress', '$post', '$phone')";
if (mysql_query($sql,$con)) {
header ("location:/done.php");
}
else {
echo "Something is wrong";
}
}//end of submit button
I fixit converting that .php file to UTF-8 without BOM
.
我修复了将该 .php 文件转换为UTF-8 without BOM
.
Thanks all for suggestions!
谢谢大家的建议!
采纳答案by iKaspars
I fixit converting that .php file to UTF-8 without BOM
.
我修复了将该 .php 文件转换为UTF-8 without BOM
.
Thanks all for suggestions!
谢谢大家的建议!
回答by qbert220
The "headers already sent" message means that your script has already output something. Is there anything displayed on the web page above this error message? Check for any whitespace before the <?php
tag. Also check any include files for whitespace before or after <?php ?>
tags.
“headers already sent”消息意味着你的脚本已经输出了一些东西。在此错误消息上方的网页上是否显示任何内容?检查<?php
标签前是否有任何空格。还要检查任何包含文件在<?php ?>
标签之前或之后的空格。
The Location header should have a space after the ":" and should be a absolute URI, similar to the following:
Location 头应该在“:”之后有一个空格,并且应该是一个绝对 URI,类似于以下内容:
header("Location: http://www.yoursite.com/done.php");
回答by Ryan Kinal
"Headers already sent" most likely means that there is some content in your .php file before your PHP code. This could be as simple as white space, or maybe your PHP code is embedded in HTML. In either case, make sure nothingcomes before your PHP code, and you should be fine.
“Headers already sent”很可能意味着 .php 文件中有一些内容位于 PHP 代码之前。这可以像空格一样简单,或者您的 PHP 代码可能嵌入在 HTML 中。无论哪种情况,请确保在您的 PHP 代码之前没有任何内容,您应该没问题。
As for the correctness of this method of redirect, I believe it is a generally accepted technique.
至于这种重定向方法的正确性,我相信这是一种普遍接受的技术。
回答by Scott
others have answered with respect to headers already sent. An alternative is to include or require done.php if the update was successful. Don't forget the exit after the include / require.
其他人已经回答了已经发送的标题。如果更新成功,另一种方法是包含或要求 done.php。不要忘记在包含/要求之后退出。
if (mysql_query($sql,$con)) {
header ("location:/done.php");
require_once('done.php');
exit();
}
else {
echo "Something is wrong";
}
回答by diEcho
use
ob_start();
at the top of page and just after <?php
使用
ob_start();
在页面的,只是后顶<?php
回答by jeroen
headers already sent
means that something has already been sent to the browser.
headers already sent
意味着某些内容已经发送到浏览器。
Is mysql giving an error?
mysql 报错了吗?
Also, note that $_POST
was introduced in PHP 4.1 so if you are using a really old version of php, that might be causing an error.
另请注意,这$_POST
是在 PHP 4.1 中引入的,因此如果您使用的是非常旧的 php 版本,则可能会导致错误。