PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
gammaman
Forum Commoner
Posts: 45 Joined: Tue Feb 12, 2008 9:22 am
Post
by gammaman » Fri Apr 18, 2008 11:40 am
I am trying to send something from one page to another using the POST method but the second page only receives it in the address bar and not in the variable it is assigned to on the second page.
The first page.
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Change A Department</title>
</head>
<?php
?>
<body>
<form action="" method="post">
User Name:<input name="user" type="text"><br />
Password:<input name="pass" type="text"><br />
<br />
<input name="Submit1" type="submit" value="submit" /><br />
</form>
</body>
</html>
<body>
<?php
$Conn=mysql_connect("localhost","fierm","13183");
if(!$Conn){
echo "failed";
}
else{
mysql_select_db("fierm");
$User = $_POST['user']; //recieves a value of one
$Pass = $_POST['pass']; //Not yet used
$result=mysql_query("select studentID, password FROM Student WHERE studentID='$User' and password='$Pass'");
$cou=mysql_num_rows($result);
if($cou>0)
{
echo "<a href=\"student.php?id=$User\">StudentPage</a>";
}
elseif(($User=="root") && ($Pass=="pwd123")){
echo "<a href=\"admin.php\">Admin Access</a>";
}
else{
// echo "Invalid Password";
}
}
?>
</body>
The second page
Code: Select all
<?php
$U=$_POST[$User];
echo $U //does not print anything but should print one
$conn=mysql_connect("localhost","fierm","13183");
if(!$conn){
echo "failed";
}
else{
mysql_select_db(fierm);
echo "<table border=\"1\">";
echo "<b>Registered Courses</b>";
echo "<tr><th>CourseID</th><th>CourseName</th><th>Drop Course</th></tr>";
$result=mysql_query("select CourseID,CourseName,StudentID FROM RegCources WHERE StudentID='$U'");
$cou=mysql_num_rows($result);
echo "$cou";
while($row=mysql_fetch_array($result))
{
echo "<tr><td>$row[0]</td><td>$row[1]</td></tr>\n";
}
echo "</table>";
}
?>
samb0057
Forum Commoner
Posts: 27 Joined: Wed Mar 26, 2008 9:51 am
Post
by samb0057 » Fri Apr 18, 2008 12:07 pm
Replace "$U=$_POST[$User];" with "$U=$_POST['user'];"
To look at the whole post array and verify if anything is coming through at all, use print_r($_POST);
gammaman
Forum Commoner
Posts: 45 Joined: Tue Feb 12, 2008 9:22 am
Post
by gammaman » Fri Apr 18, 2008 12:13 pm
If a do that all that prints is Array(), which means there is nothing in it right!
Howver if I look at the address bar I see this
http://.............../......../PHP/student.php?id=1
see the id=1, I thought that meant that the page received it, so there has to be a way to use that value.
andym01480
Forum Contributor
Posts: 390 Joined: Wed Apr 19, 2006 5:01 pm
Post
by andym01480 » Fri Apr 18, 2008 12:18 pm
Two things!
Firstly your form on the first page has no action destination, so it posts to itself. make action="loginpage.php" etc
Secondly you must have clicked the Student Page link rather than the submit button. to get the address bar as described
The line for that in your code was
Code: Select all
echo "<a href=\"student.php?id=$User\">StudentPage</a>";
That id is not a POST it's a GET so
would grab it for you.
So what is the point of the form???
gammaman
Forum Commoner
Posts: 45 Joined: Tue Feb 12, 2008 9:22 am
Post
by gammaman » Fri Apr 18, 2008 3:02 pm
Well I thought that based on my code, when the user clicks submit, it queries a database to see if the student id matches with the password, and then take that student to there own
"records" page where they can view current courses and grades. It should also check to see if the login is
root and pwd123 which means it should redirect to an admin page
gammaman
Forum Commoner
Posts: 45 Joined: Tue Feb 12, 2008 9:22 am
Post
by gammaman » Fri Apr 18, 2008 3:04 pm
andym01480 wrote: Two things!
Firstly your form on the first page has no action destination, so it posts to itself. make action="loginpage.php" etc
I don't think I can do that unless I supply the action with arguments because depending on what is inputted it will redirect to different pages.
andym01480
Forum Contributor
Posts: 390 Joined: Wed Apr 19, 2006 5:01 pm
Post
by andym01480 » Fri Apr 18, 2008 3:15 pm
Understand you!
Your first page always displays the form and you do indeed want it to post to itself.
what you need is a
Code: Select all
<?php
if (!isset($_POST['submit'])){
//show the form
exit();
}
//Processing stuff
?>
The $_POST['user'] is only available to the first page. When the user clicks on the student link
Code: Select all
echo "<a href=\"student.php?id=$User\">StudentPage</a>"; in teh first page, you are providing just an id. To use it in the second page you need
rather than
because a variable in a URL is accessed by $_GET or $_REQUEST not $_POST
Hope that is clear
califdon
Jack of Zircons
Posts: 4484 Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA
Post
by califdon » Fri Apr 18, 2008 6:18 pm
Let me try to clear some of this up. There are two methods of passing form data to a script (either another script or calling the same script again): POST and GET. POST method passes an array to the receiving script when the Submit button is clicked. GET method adds key=value pairs as part of the URL (which is visible to the user in the address bar) when the Submit button is clicked. If you call the same script again, you need to test at the beginning of the script to determine whether it is being called for the first time or as a result of clicking the Submit button, because you will use entirely different logic in either case.
Neither of these methods has anything to do with a hyperlink.
I hope that clears up your understanding so that you can take advantage of all the tips the others have given you.
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Fri Apr 18, 2008 6:40 pm
This is pretty crude but it demonstrates the logic of how to "post" and retrieve form values from within the same script.
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Change A Department</title>
</head>
<?php
if(isset($_POST['Submit1'])) //only execute if the form has been submitted
{
$Conn=mysql_connect("localhost","fierm","13183");
if(!$Conn){
echo "failed";
}
else{
mysql_select_db("fierm");
$User = $_POST['user']; //recieves a value of one
$Pass = $_POST['pass']; //Not yet used
$result=mysql_query("select studentID, password FROM Student WHERE studentID='$User' and password='$Pass'");
$cou=mysql_num_rows($result);
if($cou>0)
{
echo "<a href=\"student.php?id=$User\">StudentPage</a>";
}
elseif(($User=="root") && ($Pass=="pwd123")){
echo "<a href=\"admin.php\">Admin Access</a>";
}
else{
// echo "Invalid Password";
}
}
}
?>
<body>
<form action="" method="post">
User Name:<input name="user" type="text"><br />
Password:<input name="pass" type="text"><br />
<br />
<input name="Submit1" type="submit" value="submit" /><br />
</form>
</body>
</html>
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Fri Apr 18, 2008 7:01 pm
First things first... don't check for a submit button. What happens if a user hits enter on their keyboard when in IE? That's right, no post button click.
Second, try to understand the flow of what you are doing. Start at a page. Which page are you on? Try
Code: Select all
<?php
$thisFile = basename(__FILE__);
?>
Now you have your form action because you have the file name you are on. Post the page to itself and check for a request method
Code: Select all
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// We have a post
} else {
// We are not in post
}
?>
Handle the rest of the magic the way you want.
andym01480
Forum Contributor
Posts: 390 Joined: Wed Apr 19, 2006 5:01 pm
Post
by andym01480 » Sat Apr 19, 2008 2:56 am
Cheers for that Everah - learnt something!
gammaman
Forum Commoner
Posts: 45 Joined: Tue Feb 12, 2008 9:22 am
Post
by gammaman » Sat Apr 19, 2008 6:36 am
Thanks all of this really helps.