Quesiton about $_POST

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

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Quesiton about $_POST

Post by gammaman »

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 />
        &nbsp;<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

Re: Quesiton about $_POST

Post by samb0057 »

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

Re: Quesiton about $_POST

Post by gammaman »

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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Quesiton about $_POST

Post by andym01480 »

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

Code: Select all

$id=$_GET['id'];
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

Re: Quesiton about $_POST

Post by gammaman »

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

Re: Quesiton about $_POST

Post by gammaman »

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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Quesiton about $_POST

Post by andym01480 »

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

Code: Select all

$id=$_GET['id'];
rather than

Code: Select all

$U=$_POST[$User];
because a variable in a URL is accessed by $_GET or $_REQUEST not $_POST

Hope that is clear
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Quesiton about $_POST

Post by califdon »

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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Quesiton about $_POST

Post by aceconcepts »

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 />
         &nbsp;<input name="Submit1" type="submit" value="submit" /><br />
  
 </form>
  
 </body>
 </html>
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Quesiton about $_POST

Post by RobertGonzalez »

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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Quesiton about $_POST

Post by andym01480 »

Cheers for that Everah - learnt something!
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: Quesiton about $_POST

Post by gammaman »

Thanks all of this really helps. :D
Post Reply