php 如何跨多个页面传递多个变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20294915/
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
How to pass multiple variables across multiple pages?
提问by user1804208
My website involves a user submitting data over several pages of forms. I can pass data submitted on one page straight to the next page, but how do I go about sending it to pages after that? Here's a very simplified version of what I'm doing.
我的网站涉及用户通过多个表单页面提交数据。我可以将在一页上提交的数据直接传递到下一页,但之后如何将其发送到页面?这是我正在做的事情的一个非常简化的版本。
Page 1:
第 1 页:
<?php
echo "<form action='page2.php' method='post'>
Please enter your name: <input type='text' name='Name'/>
<input type='submit' value='Submit'/></form>";
?>
Page 2:
第2页:
<?php
$name=$_POST["Name"];
echo "Hello $name!<br/>
<form action='page3.php' method='post'>
Please enter your request: <input type='text' name='Req'/>
<input type='submit' value='Submit'/></form>";
?>
Page 3:
第 3 页:
<?php
echo "Thank you for your request, $name!";
?>
The final page is supposed to display the user's name, but obviously it won't work because I haven't passed that variable to the page. I can't have all data submitted on the same page for complicated reasons so I need to have everything split up. So how can I get this variable and others to carry over?
最后一个页面应该显示用户名,但显然它不会工作,因为我没有将该变量传递给页面。由于复杂的原因,我无法在同一页面上提交所有数据,因此我需要将所有数据分开。那么我怎样才能让这个变量和其他变量结转呢?
回答by Alexander Kimaru
Use sessions:
使用会话:
session_start();on every page
session_start();在每一页
$_SESSION['name'] = $_POST['name'];
then on page3 you can echo $_SESSION['name']
然后在第3页你可以回声 $_SESSION['name']
回答by Kerrek SB
You could store the data in a cookieon the user's client, which is abstracted into the concept of a session. See PHP session management.
您可以将数据存储在用户客户端上的cookie 中,该cookie被抽象为session的概念。请参阅PHP 会话管理。
回答by jonah
if you don't want cookies or sessions: use a hidden input field in second page and initialize the variable by posting it like:
如果您不想要 cookie 或会话:在第二页中使用隐藏的输入字段并通过发布它来初始化变量,如下所示:
page2----
$name=$_POST['name']; /// from page one
<form method="post" action="page3.php">
<input type="text" name="req">
<input type="hidden" name="holdname" value="<? echo "$name"?>">
////////you can start by making the field visible and see if it holds the value
</form>
page3----
$name=$_POST['holdname']; ////post the form in page 2
$req=$_POST['req']; ///// and the other field
echo "$name, Your name was successfully passed through 3 pages";
回答by Rebel Trouble
Use SESSION variable or hidden input field
使用 SESSION 变量或隐藏输入字段
回答by Alen ?imunic
This is my workaround of this problem: instead of manually typing in hidden input fields, I just go foreach over $_POST:
这是我解决此问题的方法:我没有手动输入隐藏的输入字段,而是通过 $_POST 进行 foreach:
foreach ($_POST as $key => $value) {
echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
Hope this helps those with lots of fields in $_POST :)
希望这可以帮助那些在 $_POST 中有很多字段的人:)
回答by Steve
As mentioned by others, saving the data in SESSION is probably your best bet.
正如其他人所提到的,将数据保存在 SESSION 中可能是您最好的选择。
Alternatly you could add the data to a hidden field, to post it along:
或者,您可以将数据添加到隐藏字段,以将其发布:
page2:
第2页:
<input type="hidden" name="username" value="<?php echo $name;?>"/>
page3
第3页
echo "hello $_POST['username'};
回答by user3050981
You can create sessions, and use posts. You could also use $_GET to get variables from the URL.
您可以创建会话并使用帖子。您还可以使用 $_GET 从 URL 获取变量。
Remember, if you aren't using prepared statements, make sure you escape all user input...
请记住,如果您不使用准备好的语句,请确保您转义所有用户输入...

