On page 3, as the data is not sent via form, it can't be read on page 3 with $_POST['username'];
$_POST[];
$_GET[];
are for form variables...
Main Topics
Browse All TopicsHi there,
I'm testing on passing parameter via url with the three php files as shown below:
1. index.php
2. pg2.php
3. pg3.php
The index.php will allow user to input something and then pass the variables to pg2.php to display.
In pg2.php, there is text url/link that will pass the variables to pg3.php and display again.
Problem: I could only pass the variables till pg2.php but not to pg3.php.
Here is the simple code that I have written:
Index.php
======
<html>
<head>
<title>Login</title>
</head>
<body onload=document.test.usern
<form method="POST" action="pg2.php" name="test">
Username: <input type="text" name="username" size="20">
Password: <input type="password" name="password" size="20"><br>
?>
<input type="submit" value="Submit" name="login">
</form>
</body>
</html>
--------------------------
pg2.php
=====
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="pg3.php" method="POST">
Username input: <?php echo $_POST['username']."<br>";
Password input: <?php echo $_POST['password']."<br><b
<a href="pg3.php?username=$_P
</form>
</body>
</html>
--------------------------
pg3.php
=====
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST">
This is page 3.<br><br>
<? php echo $_POST['username']; ?>
</form>
</body>
</html>
Please advise.
Thank you.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
you might be better off using sessions for what you are doing, here is an overview of how that would work
at the top of every page:
session_start();
to assign a variable to a session var:
$_SESSION['varname'] = $var;
for example: $_SESSION['username'] = $_POST['username'];
you can then use the session variables on any page by, for example:
echo $_SESSION['username'];
This means that you do not have to pass variables in the url query string, which will be more secure for starters. It also means you wont have to keep reassigning variables on every page that you want to pass the data through. The values of the session variables will remain all the time the session is active (ie. until the user closes their browser).
Index.php
======
<html>
<head>
<title>Login</title>
</head>
<body onload=document.test.usern
<form method="POST" action="pg2.php" name="test">
Username: <input type="text" name="username" size="20">
Password: <input type="password" name="password" size="20"><br>
<input type="submit" value="Submit" name="login">
</form>
</body>
</html>
--------------------------
pg2.php
=====
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="pg3.php" method="POST">
Username input: <?php echo $_POST['username']."<br>";
Password input: <?php echo $_POST['password']."<br><b
<a href="pg3.php?username=$_P
</form>
</body>
</html>
--------------------------
pg3.php
=====
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST">
This is page 3.<br><br>
<? php echo $username; ?>
</form>
</body>
</html>
Actually doesn't much difference, I just changed the line that you mentioned to me.
Thank you.
Page 2 should loke like this:
pg2.php
=====
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="pg3.php" method="POST">
Username input: <?php echo $_POST['username']."<br>";
Password input: <?php echo $_POST['password']."<br><b
<a href="pg3.php?username=<?p
</form>
</body>
</html>
Since the value you are giving to the href is a PHP variable you need to print it in PHP environment... ;)
That should do it...
altric, I still think you would be better off using sessions so here is a quick session solution if ever you decide to go this way
Index.php
======
<?php
session_start();
?>
<html>
<head>
<title>Login</title>
</head>
<body onload=document.test.usern
<form method="POST" action="pg2.php" name="test">
Username: <input type="text" name="username" size="20">
Password: <input type="password" name="password" size="20"><br>
?>
<input type="submit" value="Submit" name="login">
</form>
</body>
</html>
--------------------------
pg2.php
=====
<?php
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="pg3.php" method="POST">
Username input: <?php echo $_SESSION['username']."<br
Password input: <?php echo $_SESSION['password']."<br
<a href="pg3.php">Go to Page 3</a>
</form>
</body>
</html>
--------------------------
pg3.php
=====
<?php
session_start();
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST">
This is page 3.<br><br>
<?php echo $_SESSION['username']; ?>
</form>
</body>
</html>
First, if you want to submit via the url-string, you have to use the GET method, not POST.
Second, on page two, you have to explicitly submit the form to pass the variable. Your link must include a command to submit the form, such as:
<a href="javascript:'onload="
the form's action will successfully execute the link to pg3.php.
>> Second, on page two, you have to explicitly submit the form to pass the variable.
On the second page there is not actually any form fields to pass, the reason I did not remove them in the example i provided was because i presumed there was the possibility that altric will add form fields at a later time.
Further more, if form fields are added then you can presume that this will also include a submit button, using the sessions method this will not cause any problems or require much change to the code.
If altric wishes to continue to use the GET method its still possible, although much more inconvienient, as the query string can be added to the forms action.
diablo,
I agree session variables are more convenient, but the question said "in the url", so I assumed it was an exercise in passing variables via forms, not as session variables, so I was sticking with that method, which requires a submission of the form variables from page2 to page3. That was the error in the original submission.
Business Accounts
Answer for Membership
by: stefanaichholzerPosted on 2004-06-25 at 22:29:27ID: 11404188
In page 2 you are not using the form to pass the data, you using the hyperlink, so on page 3 just read the variable like this:
pg3.php
=====
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST">
This is page 3.<br><br>
<? php echo $username; ?>
</form>
</body>
</html>
If you want to keep sending the data with the form, then send the data from page 2 with the form, a link is not part of the form...
Hope this helps for you... ;)