Page 1 of 1

Storing Variables In Session()

Posted: Thu Mar 12, 2009 3:53 pm
by nishmgopal
Hi Guys please refer to my code below:

Code: Select all

$query = "SELECT * FROM Job_ID";
    $result = mysql_query($query);
    
    echo "<h1 class='title2'>Upcoming Project Roles</h1>
    <p>From the menu below please select the Project Role:</p>
     
    <form id='form2' method='post' action='new_go.php'>
   <p>
    <label>
 <select name='job' id='job'>";
    
[color=#BF0000] $Job_ID = $row['Job_ID'];[/color] - [color=#40FF00]HOW CAN I STORE THIS IN SESSION?[/color]
       $Project_Name= $row['Job_Name'];
       
       echo "<option value=\"$Project_Name\" \"$Job_ID\">$Job_ID - $Project_Name</option>";
   }
   
 echo "</select></label></p>
  <input type='submit' name='notjob' id='button' value='Go' />
 </label>
 </p>
 </form>
";
 

Re: Storing Variables In Session()

Posted: Fri Mar 13, 2009 6:54 am
by nishmgopal
can anyone please help?

Re: Storing Variables In Session()

Posted: Fri Mar 13, 2009 7:24 am
by mattpointblank
Underneath it (before any output is displayed):

Code: Select all

 
session_start();
$_SESSION['Job_ID'] = $Job_ID;
 
You can now use this variable ($_SESSION['Job_ID']) on any page on your site, as long as you call session_start() beforehand. Really though, a quick search on Google or the PHP manual site would have told you this.

Re: Storing Variables In Session()

Posted: Fri Mar 13, 2009 8:01 am
by nishmgopal
I have already tried this and it didnt work. the top of my page where the variable is coming from looks like:

Code: Select all

<?php 
 
session_start();
$_SESSION['Job_ID'] = $Job_ID;
 
?>
and on everyother page where I want to use this variable looks like this:

Code: Select all

 <?php
 
 session_start();
$_SESSION['Job_ID']; 
 ?>
and to use this variable:

Code: Select all

<div class="title2"> <? echo $_SESSION['Job_ID'] ?> Specification:</div>
This doesnt work. What am i doing wrong?

Re: Storing Variables In Session()

Posted: Fri Mar 13, 2009 8:17 am
by nishmgopal
I have figured out the problem! Got it to work

But now I am having trouble callin the variable $Project_Name, this is what my code looks like:

Code: Select all

$Job_ID = $row['Job_ID'];
       session_start();
       $_SESSION['Job_ID'] = $Job_ID;
       $Project_Name= $row['Job_Name'];
       $_SESSION['Job_Name'] = $Project_Name;
I can call the $Job_ID by using $_SESSION['Job_ID'] = $Job_ID, but when I try to use $_SESSION['Job_Name'] = $Project_Name it doesnt work.

Re: Storing Variables In Session()

Posted: Fri Mar 13, 2009 8:38 am
by susrisha
post any errors that you are getting and add an echo before assigning to session to see whats the value that you are getting in $Project_Name

Re: Storing Variables In Session()

Posted: Fri Mar 13, 2009 8:50 am
by nishmgopal
I think I should explain the whole system again, as even my $Job_ID is not working as I would want.

I have page 1 where a drop down menu is generated by my sql statement, the code looks like:

Code: Select all

$query = "SELECT * FROM Job_ID";
    $result = mysql_query($query);
    
    echo "<h1 class='title2'>Upcoming Project Roles</h1>
    <p>From the menu below please select the Project Role:</p>
     
    <form id='form2' method='post' action='new_go.php'>
   <p>
    <label>
 <select name='job' id='job'>";
    
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
       $Job_ID = $row['Job_ID'];
       session_start();
       $_SESSION['Job_ID'] = $Job_ID;
       $Project_Name= $row['Job_Name'];
 
       
       echo "<option value=\"$Project_Name\" \"$Job_ID\">$Job_ID - $Project_Name</option>";
   }
The on Submit, the action goes to page2, where I want to display all the records from the job table where the Job ID is passed from Page 1 ($Job_ID). At first I thought this was working, but when I select different records from the drop down menu and hit submit, the same value keeps showing. the code for page 2:

Code: Select all

 
        
     <?php
 
 session_start();
$_SESSION['Job_ID']; 
 ?>
 
$query1 = "SELECT * FROM Job_ID WHERE Job_ID ='$_SESSION[Job_ID]'";
            
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query.");
 
while($row = mysql_fetch_assoc($result1)){
  $array[] = $row; }
     
array2table($array,200);
 

Re: Storing Variables In Session()

Posted: Fri Mar 13, 2009 12:06 pm
by nishmgopal
please help!