Php 如何在多个页面上传递多个变量?

Php 如何在多个页面上传递多个变量?,php,html,form-submit,Php,Html,Form Submit,我的网站涉及到一个用户通过几页表单提交数据。我可以将一页上提交的数据直接传递到下一页,但在这之后如何将其发送到下一页?这是我正在做的一个非常简化的版本 第1页: <?php echo "<form action='page2.php' method='post'> Please enter your name: <input type='text' name='Name'/> <input type='submit' value='Submit'/>&l

我的网站涉及到一个用户通过几页表单提交数据。我可以将一页上提交的数据直接传递到下一页,但在这之后如何将其发送到下一页?这是我正在做的一个非常简化的版本

第1页:

<?php
echo "<form action='page2.php' method='post'>
Please enter your name: <input type='text' name='Name'/>
<input type='submit' value='Submit'/></form>";
?>

您可以将数据存储在用户客户端的cookie中,该cookie被抽象为会话的概念。请参阅。

使用会话:

session_start()在每一页上

$_SESSION['name'] = $_POST['name'];

然后在第3页上,您可以回显会话['name']

,正如其他人提到的,在会话中保存数据可能是您的最佳选择

或者,您可以将数据添加到隐藏字段中,以便将其发布:

第2页:

 <input type="hidden" name="username" value="<?php echo $name;?>"/>

您可以创建会话和使用帖子。您还可以使用$\u GET从URL获取变量


请记住,如果您没有使用准备好的语句,请确保退出所有用户输入…

如果您不需要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";
page2----
$name=$_POST['name'];//从第一页开始

使用会话变量或隐藏输入字段

这是我解决此问题的方法:我不需要手动键入隐藏输入字段,而只需在$\u POST上查找每个字段:

foreach ($_POST as $key => $value) {
    echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
foreach($\u发布为$key=>$value){
回声';
}

希望这能帮助那些在$\u POST:)中有很多字段的人。

除了技术可行性之外,这还带来了一些可用性问题。我建议把它全部做成一种形式,而不是多次点击。那不是session_start()吗?@JasonStack:是的!哇,4年后!
echo "hello $_POST['username'};
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";
foreach ($_POST as $key => $value) {
    echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}