Page 1 of 1

Quesiton about $_POST

Posted: Fri Apr 18, 2008 11:40 am
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>";
}
?>    
 

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 12:07 pm
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);

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 12:13 pm
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.

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 12:18 pm
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???

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 3:02 pm
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

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 3:04 pm
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.

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 3:15 pm
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

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 6:18 pm
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.

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 6:40 pm
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>
 

Re: Quesiton about $_POST

Posted: Fri Apr 18, 2008 7:01 pm
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.

Re: Quesiton about $_POST

Posted: Sat Apr 19, 2008 2:56 am
by andym01480
Cheers for that Everah - learnt something!

Re: Quesiton about $_POST

Posted: Sat Apr 19, 2008 6:36 am
by gammaman
Thanks all of this really helps. :D