Unable to pass variables from one form to another

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
tmcmahan
Forum Newbie
Posts: 4
Joined: Tue Aug 31, 2010 1:25 pm

Unable to pass variables from one form to another

Post by tmcmahan »

I am a beginner with PHP programming. I am trying to develop an on line supply requisition system that will allow users to be authenticated by a login form and then be presented with a supply order form to complete. I need to pass the “user name” and “EmpNo” to the supply form but I am unable to pass variables from one form to another. The following portion of code from the login.php. In this form on line 18 I have tried to pass the employee number “EmpNo” but I am unable to place a variable into an HTML input value. Is this possible or is there another way.

1 $i=0;
2 while ($i<$num){
3
4 if($user==mysql_result($result,$i,"username") && $pw==mysql_result($result,$i,"password"))
5 {
6 $validuser = "Y";
7 $first=mysql_result($result,$i,"first");
8 $last=mysql_result($result,$i,"last");
9 $EmpNo=mysql_result($result,$i,"EmpNo");
10 }
11 $i++;
12 }
13 if($validuser=="Y")
14 {
15
16 echo "<h1><b><center>WELCOME</center></b></h1>";
17 echo "<h2><b><center>$first $last</center></b></h2>";
18 echo "<h2><b><center>$EmpNo</center></b></h2>";
19 echo '<input type="text" name="EmpNo" size="8" value= $EmpNo />';
20
21 include 'SupplyOrder.php';
22 }
23 else
24 echo "<b><center>Invalid User\Password</center></b>";
25 ?>
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Unable to pass variables from one form to another

Post by shawngoldw »

I assume you're using sessions for the login?
You can just define session variables with the information you need to pass.

Shawn
tmcmahan
Forum Newbie
Posts: 4
Joined: Tue Aug 31, 2010 1:25 pm

Re: Unable to pass variables from one form to another

Post by tmcmahan »

I am so new that I did not know of session variables. I have done some research and have been able to successfully transfer the variables from form to from. Thank you so much for your help…
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Unable to pass variables from one form to another

Post by shawngoldw »

No problem, if you weren't using sessions, how were you doing the logins?
tmcmahan
Forum Newbie
Posts: 4
Joined: Tue Aug 31, 2010 1:25 pm

Re: Unable to pass variables from one form to another

Post by tmcmahan »

Not sure this is the excepted method.. I just looped through the Employee table to determine if the userid and password on the index.hrml match any row in the employee table. If a match exists then load the supply form.

$user =$_POST['userid'];
$pw =$_POST['password'];
$validuser = "N";

$i=0;
while ($i<$num){
if($user==mysql_result($result,$i,"username") && $pw==mysql_result($result,$i,"password"))
{
$validuser = "Y";
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$EmpNo=mysql_result($result,$i,"EmpNo");
$_SESSION['EmpNo1'] = $EmpNo;
}
$i++;
}
if($validuser=="Y")
{
echo '<form action="SupplyOrder.php" method="post">';
echo "<h1><b><center>WELCOME</center></b></h1>";
echo "<h2><b><center>$first $last</center></b></h2>";
echo '</form>';
include 'SupplyOrder.php';
}
else
echo "<b><center>You must enter a valid userid and password</center></b>";
?>
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Unable to pass variables from one form to another

Post by tonchily »

tmcmahan wrote:Not sure this is the excepted method.. I just looped through the Employee table to determine if the userid and password on the index.hrml match any row in the employee table. If a match exists then load the supply form.

$user =$_POST['userid'];
$pw =$_POST['password'];
$validuser = "N";

$i=0;
while ($i<$num){
if($user==mysql_result($result,$i,"username") && $pw==mysql_result($result,$i,"password"))
{
$validuser = "Y";
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$EmpNo=mysql_result($result,$i,"EmpNo");
$_SESSION['EmpNo1'] = $EmpNo;
}
$i++;
}
if($validuser=="Y")
{
echo '<form action="SupplyOrder.php" method="post">';
echo "<h1><b><center>WELCOME</center></b></h1>";
echo "<h2><b><center>$first $last</center></b></h2>";
echo '</form>';
include 'SupplyOrder.php';
}
else
echo "<b><center>You must enter a valid userid and password</center></b>";
?>
That can go through, but if a user refreshes the page he will have to log in again. That's why sessions are for. I recommend you to read some tutorials on sessions.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Unable to pass variables from one form to another

Post by shawngoldw »

Like tonchilly said you should read about sessions.

You can simplify the login process greatly too.
Rather than looping through every record in your login table, you can do something like this:

Code: Select all

$pass = mysql_real_escape_string($_POST['password']);
$user = mysql_real_escape_string($_POST['userid']);

$sql = "SELECT * FROM `table` WHERE `username`='username' AND `password`='password'";
$result = mysql_query($sql);
$num = mysql_num_row($result);

$logged_in = false;
if($num == 1)
{
  $user = mysql_result($result, 0, 'username');
  $first = mysql_result($result, 0, 'first');
  $last = mysql_result($result, 0, 'first');
  $EmpNo = mysql_result($result, 0, 'first');
  $logged_in = true;
}
This will need some tweaking to make work for you. You would then use sessions to store that the login was successful and the data relevant.

If you have any questions about it feel free to ask,
Shawn
tmcmahan
Forum Newbie
Posts: 4
Joined: Tue Aug 31, 2010 1:25 pm

Re: Unable to pass variables from one form to another

Post by tmcmahan »

I can see that I have a long way to go. Thanks for all of your help...
Post Reply