Reputation: 1253
If I have three PHP pages, lets call them one.php, two.php, and three.php, and I use something like this on the first page:
<form name = "one" action = "two.php" onsubmit="return validateForm();" method ="post">
I understand that I can now use the variables I passed alone from one.php in two.php...
My question is, how would I use the variables from one.php in three.php?
I keep getting "undefined index" errors every time I try to call them as I would in two.php.
Thank you very much for your help!
Edit: I should have mentioned that I am using Sessions as well.
On the second page, I do something like this:
$_SESSION['name'] = $_POST['name'];
and it lets me pass on the name gathered from that first page to the second one, but when I attempt to do this on the third page, it just says "undefined index".
Upvotes: 0
Views: 171
Reputation: 102743
Just make sure your input elements are named correctly. If you want to use $_POST["myvar"]
in two.php, then the name
attribute in one.php needs to be "myvar".
<form action="two.php" method="post">
<input type='text' value='test myvar' name='myvar' />
<input type='submit' value='go to 2' />
</form>
To use the variables from one.php in three.php, just pass them through as hidden inputs. That is, echo them back to the form in two.php:
<form action="three.php" method="post">
<input type='text' value='test var 2' name='myvar2' />
<?php echo "<input type='hidden' name='myvar' value='" . $_POST["myvar"] . "' />"; ?>
<input type='submit' value='go to 3' />
</form>
Upvotes: 0
Reputation: 943163
I understand that I can now use the variables I passed alone from one.php in two.php...
Not as such.
Successful form controls in the form will submit their values to two.php
. You can then access them via $_POST['name_of_control']
.
If you generate form controls (possibly of type hidden) with values created from PHP variables (making sure to escape them to defend against XSS attacks) then they will appear in the form and be submitted.
My question is, how would I use the variables from one.php in three.php?
That depends on how the browser gets to three.php. You could generate another form and put the values in hidden inputs again.
Upvotes: 1
Reputation: 36299
Instead of using post, you would need to use $_SESSION
so for example:
one.php
session_start();
$_SESSION["mypostvalue"] = $_POST["mypostvalue"];
$_SESSION["mypostvalue1"] = $_POST["mypostvalue1"];
three.php
session_start();
echo $_SESSION["mypostvalue"];
echo "<br />";
echo $_SESSION["mypostvalue1"];
Then once you are done with the session data, you need to get rid of it like this:
session_start();
$_SESSION = null;
session_destory();
Note Only use session_start() once per page, and before any data is sent to the browser such as html or white space.
Upvotes: 2
Reputation: 550
$_POST is per request You should pass them to the third file using $_SESSION or $_GET.
Upvotes: 0