how to use session()

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

nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

Im absolutley baffled!

Nothing seems to be working, I am sure there is a small mistake some where! I am going to post the full code for both pages just in case someone can notice somehting which I have missed:

For simplicity I will refer to them has Page 1 and Page 2

Page1:

Code: Select all

<?php 
 
session_start();
 
if(isset($_POST['job']))
 {
 $_SESSION ['job'] = $_POST['job'];
 }
?>
 
<?php
// connects to Database
 
$host="co-project";
$user="conmg";
$password="****";
 
$conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql');
$dbname="conmg";
 
$dbname=@mysql_select_db($dbname, $conn)
           or die("Couldn't select database");
           ////
           
     $query = "SELECT * FROM Job_Spec";
    $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='go.php'>
   <p>
    <label>
 <select name='job' id='job'>";
    
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
       $Project_Name= $row['Project_Name'];
       echo "<option value=\"$Project_Name\">$Project_Name</option>";
   }
   
 echo "</select></label></p>
  <input type='submit' name='notjob' id='button' value='Go' />
 </label>
 </p>
 </form>
";
  
  ?>
Page 2:

Code: Select all

 
<?php
 
session_start(); 
    
$getjob = $_SESSION['job'];
    
    ?>
 
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
 
            
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query.");
 
$colcnt = 0;
 
 
   while ($row = mysql_fetch_array($result1,MYSQL_ASSOC)) {
  {
     if (!$colcnt)
     {     
          $colcnt = 7;    
    }
 
   $colcnt--;{
   
      $Name1 = $row['Project_Name'];
      $Java1 = $row['Java'];
      $Leadership1 = $row['Leadership'];
      $Communication1 = $row['Communication'];
      $Teamwork1 = $row ['Team_Work'];
      $Problem_Solving1 = $row['Problem_Solving'];
      }
}
 
echo "$Java1";
 
?>
 
Above code returns a blank screen!! :(
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

Put this in page2.php and see if the first echo turns up.

Code: Select all

 
 $getjob = $_SESSION['job'];//this might be the clue
 

Code: Select all

 
if(!isset($_SESSION['job']))
{
echo "Job is not set yet";
}
else
{
//your rest of the code
}
 
Yes my assumption is that you wrote the action php in the same form. ie., the page1.php is go.php

That means you have to atleast submit once to get the value into the session.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

Yes it returs the echo - job is not yet set.

Page 1 looks like this:

Code: Select all

 
$query = "SELECT * FROM Job_Spec";
    $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='go.php'>
   <p>
    <label>
 <select name='job' id='job'>";
    
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
       $Project_Name= $row['Project_Name'];
       echo "<option value=\"$Project_Name\">$Project_Name</option>";
   }
   
 echo "</select></label></p>
  <input type='submit' name='notjob' id='button' value='Go' />
 </label>
 </p>
 </form>
 
So here my action is 'go.php' when the submit button is hit. go.php is the results of the query.

Go.php looks like this:

Code: Select all

$conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql');
$dbname="conmg";
 
$dbname=@mysql_select_db($dbname, $conn)
           or die("Couldn't select database");
           ////
 
$getjob=$_POST['job'];
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
            
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query.");
 
while ($row1=mysql_fetch_array($result1)){
 
        $name1=$row1['Project_Name'];
        $skill1=$row1['Java'];
        $skill2=$row1['Team_Work'];
        $skill3=$row1['Leadership'];
        $skill4=$row1['Communication'];
        $skill5=$row1['Problem_Solving'];
 
$total = ($skill1 + $skill2 + $skill3 + $skill4 + $skill5);
 
 
echo "<table width='200' border='1'>
<strong> $name1<br></strong> <br></br>
  <tr bgcolor='#4096EE'> <div align='center'>
    <td><div align='center'><strong>Skill</strong></div></td>
    <td><div align='center'><strong>Required Score</strong></div></td>
  </tr>
  <tr>
    <td>Teamwork</td>
    <td>$skill2</td>
  </tr>
  <tr>
    <td>Leadership</td>
    <td>$skill3</td>
  </tr>
  <tr>
    <td>Java</td>
    <td>$skill1</td>
  </tr>
    <tr>
    <td>Communication</td>
    <td>$skill4</td>
  </tr>
    <tr>
    <td>Problem_Solving</td>
    <td>$skill5</td>
  </tr>
    <tr bgcolor='#85C329'> <div align='center'>
    <td><strong>Total</td>
    <td><strong>$total</td>
  </tr>
  <tr>
   </tr>
</table>
<form id='form2' method='post' action='page2.php'>
              <p>
                <label>
                  <input type='submit' name='find_person' id='find_person' value='Submit' />
                </label>
              </p>
            </form>
 
";
              }
        ?>
The submit button has the action: page2.php.

and page2.php is where my sql query is not working.

On go.php you will notice I get the variable 'job' via POST method:

$getjob=$_POST['job'];
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";

and this works fine. But even on go.php if I remove the POST function and just rely on the session variable again I get a blank screen!

I really cant figure this out!
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

sorry, on the go.php I also have:

Code: Select all

<? session_start(); 
    
    $getjob = $_SESSION['job'];
    
    ?>
 
but this seems to be doing nothing!
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

okay nish i got what seams to be your problem.

1. You cannot assign the value into session in page1.php because you will be able to get the value of $_POST['job'] on the same page even if you submit.

2. The only place where you can put the session variable for $_POST['job'] is in go.php
3. So in go.php at the start,

Code: Select all

 
<? session_start();
   
    $getjob = $_POST['job'];
$_SESSION['job']  = $getjob;
   
    ?>
 
Now you can access the variable job in your page2.php as $_SESSION['job']


To be more clear

Page1.php----->$_POST['job']----> go.php----->$_SESSION['job']---->page2.php
Wish i could draw diagram to explain you but i am poor at that.. hope this helps
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

I have put the following code at the start of go.php:

Code: Select all

 
<?php
 
  $getjob = $_POST['job'];
 $_SESSION['job']  = $getjob;
    
    
    ?>
 
and the top of page2.php I have:

Code: Select all

<?php
 
session_start(); 
    
    ?>
            <h1 class="title2">Find Suitable Candidate: <?php echo $_SESSION['job'] ?></h1>
 
But again It will not echo the job...

then I tried this on page2.php

Code: Select all

 
session_start(); 
    
$_SESSION['job'];
    
    ?>
            <h1 class="title2">Find Suitable Candidate: <?php echo $_SESSION['job'] ?></h1>
 
Again with no luck!

I am sorry about this but appreciate the help so much!
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: how to use session()

Post by JeffG »

(a) You don't have a session_start() in go.php.
(b) Your two versions of page2.php are the same. The $_SESSION['job']; at line 4 of the second version is doing nothing.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

okay now here are the few things i want you to do

1. page1.php==> Delete all the php code .. its of no use there i suppose.
2. go.php

Code: Select all

 
<?php
session_start();
if(isset($_POST['job']))
{
echo post value assigned to job is $_POST['job'];
$_SESSION['job']=$_POST['job'];
echo SESSION VALUE ASSIGNED is $_SESSION['job'];
}
else
{
echo "POST VALUE NOT ASSIGNED TO JOB";
}
?>
 
3. page2.php

Code: Select all

 
<?php
session_start();
if(!isset($_SESSION['job']))
{
echo "job value is not set";
}
else
{
echo "JOB Value is set to $_SESSION['job']";
}
?>
 
Then tell us whats the error you are getting..
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

Thank you for your help, I will try this and report to you what errors I get.

Thank you so much!
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

this is what my go.php looks like now:

Code: Select all

 
<?php
 session_start();
if(isset($_POST['job']))
 {
 echo "post value assigned to job is $_POST['job']";
 }
 
 $_SESSION['job']=$_POST['job'];
 
 {
 echo "SESSION VALUE ASSIGNED is $_SESSION['job']";
 }
 else
 {
 echo "POST VALUE NOT ASSIGNED TO JOB";
}
 ?>
 
and I get the error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /disks/diskh/zco/conmg/public_html/sessiontest.php on line 13
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

Code: Select all

 
 session_start();
if(isset($_POST['job']))
 {
 echo "post value assigned to job is $_POST['job']";
 //} //remove this please
 
 $_SESSION['job']=$_POST['job'];
 
// { //remove this please
 echo "SESSION VALUE ASSIGNED is $_SESSION['job']";
 }
 else
 {
 echo "POST VALUE NOT ASSIGNED TO JOB";
}
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

I am still getting the exact same error on go.php. My code is shown below:

Code: Select all

<?php 
session_start();
if(isset($_POST['job']))
 {
 echo "post value assigned to job is $_POST['job']";
  }
 
  $_SESSION['job']=$_POST['job'];
 
  echo "SESSION VALUE ASSIGNED is $_SESSION['job']";
  }
  else
  {
  echo "POST VALUE NOT ASSIGNED TO JOB";
 }
 
?>
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

line 6 please remove the curly brace "}"
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: how to use session()

Post by nishmgopal »

still the same error. Here is the code:

Code: Select all

 
<?php 
session_start();
if(isset($_POST['job']))
 {
 echo "post value assigned to job is $_POST['job']";
 
 
  $_SESSION['job']=$_POST['job'];
 
  echo "SESSION VALUE ASSIGNED is $_SESSION['job']";
  }
  else
  {
  echo "POST VALUE NOT ASSIGNED TO JOB";
 }
 
?>
 
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how to use session()

Post by susrisha »

post the exact error again please.
Post Reply