php 表单提交后PHP重定向到新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21730854/
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
PHP Redirect to new page after form submission
提问by Ezio_
I have a form, which redirects the user to "page1.php" after the form is submitted. What I want to do is to redirect the user to "page2.php" after the form is submitted, but I need to make sure that the POST request was sent. Example:
我有一个表单,它在提交表单后将用户重定向到“page1.php”。我想要做的是在表单提交后将用户重定向到“page2.php”,但我需要确保发送了POST请求。例子:
<form action="page1.php" method="POST">
<input type="text" name="username" />
<input type="text" name="age" />
<input type="submit" value="" />
</form>
When the user clicks on Submit, it redirects him to page1.php. I want to redirect him to page2.php, but I need to make sure that the data is sent to the server. I can't use AJAX. Is there any way to do it with cURL or something like that? Any examples?
当用户单击提交时,它会将他重定向到 page1.php。我想将他重定向到 page2.php,但我需要确保将数据发送到服务器。我不能使用 AJAX。有没有办法用 cURL 或类似的东西来做到这一点?有什么例子吗?
Thanks!
谢谢!
回答by Suman K.C
I guess this works !! In page1.php
我想这有效!!在 page1.php
<?php
//do establish session
//and check for the input fields obtained via $_POST
if(isset($_POST['name_of_your_field']) && !empty($_POST['name_of_your_field'])){
if(!mail($to,$subject,$message)){
header('location:form.php?msg=error');
}else{
header('location:page2.php?msg=succes');
}
}
?>
回答by realshadow
You can check if the POST request was sent with something like:
您可以检查 POST 请求是否是通过以下方式发送的:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// do something...
}
You can create a hidden input in your form and send additional info about the form that is submitted, e.g. action.
您可以在表单中创建隐藏输入并发送有关提交的表单的其他信息,例如操作。
Inside you will do your magic and redirect user with:
在里面,你会做你的魔术并重定向用户:
header('Location: page2.php');
exit();
回答by Immortal Dude
you could do a check if query is complete
您可以检查查询是否完成
example
例子
<?php
$query=mysqli_query(".......your query statement")or trigger_error(mysqli_error());
if($query){
header("Location:page2.php");
}
else{
die('error');
}
?>
回答by Quixrick
In your 'page1.php' processor, add a 'header' redirect to 'page2.php'.
在您的“page1.php”处理器中,添加一个“header”重定向到“page2.php”。
header("Location: page2.php");
exit;
回答by Ulysses
In your situation, you can just simple check for the posted data. Like
在您的情况下,您可以简单地检查发布的数据。喜欢
$username = $_POST['username'];
$age = $_POST['age'];
if($username&&$age){ /* This is to check if the variables are not empty */
// redirect to page2
}
It is logical that if those are not empty, meaning they are posted. Even if they are empty, as long as you get there, that means it was posted. There is no need to check if posted or not, what needs to be checked was, if posted data was there.
如果这些不为空,则表示它们已发布,这是合乎逻辑的。即使它们是空的,只要您到达那里,就意味着它已发布。不需要检查是否已发布,需要检查的是,是否有发布的数据。
I hope I made it clear. ^_^ but making sure is not bad at all. Happy coding friend.
我希望我说清楚了。^_^ 但确保一点也不差。快乐的编码朋友。

