Page 1 of 1

send drop down list data to session variable

Posted: Sun Mar 02, 2008 11:27 am
by kri6ck
Hello,

I've been trying all day but I don't find the solution to this:
I have a page called identification.php on which the user selects his name in a drop down list (first and last name coming from an MySQL query) and types in a password. This data is treated by another script called welcomepage.php.

I want the welcomepage script to take the selected name from the drop down list and - if name and password are correct - put it into a session variable named $person and then show a message like "Hello $person". My problem is that when I call the session variable, the welcomepage shows me the id value (an integer) instead of the name of the drop down list. Thanks in advance for your help !!!

I have the following code in my identification.php script (important parts in red):

Code: Select all

<?php
#connecting to the database
define("SERVER", "localhost") ;
define("USER", "root") ;
define("PASSWORD", "whatever") ;
define("BASE", "mydatabase") ;
 
$connection = mysql_connect("localhost", "root", "whatever") or die ("connection impossible");
 mysql_select_db("mydatabase", $connection) or die ("database selection impossible");
 
#mysql query
[color=#BF0000]$query = "SELECT id, name, firstname, password FROM identification" ;[/color]
$result = mysql_query($query) ; 
 
while($row = mysql_fetch_object($result))
  [color=#BF0000] { $tab_pers[$row->id] = "$row->firstname $row->name" ; }[/color]
    mysql_close($connection) ;
 
#the html document head
echo "<HTML>\n<HEAD><TITLE>identification form</TITLE></HEAD>\n<BODY>\n" ;
 
#the html document title
echo "<h1 align=\"center\"> Identification </h1>\n" ;
 
#the form with a drop down list showing first and last name and a textfield for entering a password
echo "<FORM action=\"welcomepage.php\" method=\"POST\">\n
      <CENTER>\n<BR><BR>\n
      <TABLE>\n<TR><TD align='right'>log in :</TD><TD>" ;
    
    echo "[color=#BF0000]<SELECT name=\"person\">\n" ;[/color]
        [color=#BF0000]foreach($tab_pers as $key=>$val)[/color]
        [color=#BF0000] { echo "<OPTION value=\"$key\">$val </OPTION>\n" ; }[/color]
        [color=#BF0000]echo "</SELECT>\n" ;[/color]
 
    echo "</TD></TR><TR><TD align='right'>password : </TD>
            <TD><INPUT type=\"password\" name=\"pass\" maxlength=6></TD></TR>
            </TABLE><BR>\n
            <INPUT type=\"submit\" value=\"ok\">\n
            </CENTER>\n</FORM>" ;
 
#end of the Html document
echo "</BODY>\n</HTML>\n" ;
?>


the welcomepage.php script (important parts in red):

Code: Select all

<?php
 
[color=#BF0000]if (isset ($_POST['pass'])) {
    [color=#BF0000]$person = $_POST['person'] ;[/color]
    [color=#BF0000]$pass = $_POST['pass'];[/color]
   [color=#BF0000] } [/color]
[color=#BF0000]else {[/color]
   [color=#BF0000] "33";   #define some default value[/color]
    [color=#BF0000] }[/color][/color]
 
#connecting to the database
$connection = mysql_connect("localhost", "root", "whatever") or die ("connection impossible");
 mysql_select_db("mydatabase", $connection) or die ("database selection impossible");
 
#the mysql query
$query = "SELECT * FROM identification WHERE id='$person' AND pass='$pass'";
$result = mysql_query($query) ;
 
if($result <> FALSE){
  if(mysql_num_rows($result) > 0)  
    
    { session_start() ;
      [color=#BF0000]$_SESSION['person'] = $person ; 
      $row=mysql_fetch_object($result) ;[/color]
     
      #the html document
    echo "<HTML>\n<HEAD><TITLE>Welcome</TITLE></HEAD>\n<BODY>\n" ;
    [color=#BF0000]echo "<h1 align=\"center\">Hello $person !</h1>\n" ;[/color]
     
      #end of the Html document
    echo "</BODY>\n</HTML>\n" ;
    }
 
  else{
     echo "<H3>No identification possible. <A HREF=\"identification.php\">Retry ! </A></H3>\n";
     }
  }
else {
  echo "<H3> identification problem</H3>\n" ;
  }
 
mysql_close($connection) ;
?>

Re: send drop down list data to session variable

Posted: Mon Mar 24, 2008 1:42 am
by wakaman
on youe query:
#mysql query
$query = "SELECT id, name, firstname, password FROM identification" ;
$result = mysql_query($query) ;

while($row = mysql_fetch_object($result))
{ $tab_pers[$row->id] = "$row->firstname $row->name" ; }
mysql_close($connection) ;

Try starting with name or firstname instead of ID, I think your result is picking up the first query identifier, which is id. Hope this helps,