问题
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:
<?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:
<?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:
<?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?
回答1:
Use sessions:
session_start();
on every page
$_SESSION['name'] = $_POST['name'];
then on page3 you can echo $_SESSION['name']
回答2:
You could store the data in a cookie on the user's client, which is abstracted into the concept of a session. See PHP session management.
回答3:
if you don't want cookies or sessions: use a hidden input field in second page and initialize the variable by posting it like:
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";
回答4:
As mentioned by others, saving the data in SESSION is probably your best bet.
Alternatly you could add the data to a hidden field, to post it along:
page2:
<input type="hidden" name="username" value="<?php echo $name;?>"/>
page3
echo "hello $_POST['username'};
回答5:
You can create sessions, and use posts. You could also use $_GET to get variables from the URL.
Remember, if you aren't using prepared statements, make sure you escape all user input...
回答6:
Use SESSION variable or hidden input field
来源:https://stackoverflow.com/questions/20294915/how-to-pass-multiple-variables-across-multiple-pages