Jump to content

trouble with session vars not transfering over correctly to form submittal page


webguync

Recommended Posts

Hello,

 

I have a login page, which takes you to a form grid where you can select from a list, and choose checkboxes for individuals and when you submit the results are displayed on a page and also entered into a MySQL DB. The results part works fine in terms of showing which checkboxes were checked, and they are entered into a table showing the ID of the checkboxes checked. What doesn't work correctly is showing the correct information from the select menu. So for instance, you select John Doe, on the results page it may show that the results are for Jane Doe, which is another select option. In the database it may also show the employee as being Jane Doe, instead of John Doe. Again, the actual boxes checked is correct, it's just an issue with the select menu session vars displaying correctly on my results page. My code for the two pages is below.

 

page with checkboxes and select form

<?php
session_start();
if(($_SESSION['login'] == 1)  && isset($_SESSION['Assessor'])) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Novo Nordisk Selling Skills Recommended Follow-up</title>
<link rel="stylesheet" href="Global.css" type="text/css" media="screen, handheld" />
</head>
<body>
<?php
/*--------- DATABASE CONNECTION INFO--------- */
//set up table and database names
$db_name ="DBName";
$grid_name ="grid";
$employee_table="employees";
$check_table = "emp_checks";
//connect to server and select database
$connection = @mysql_connect("localhost" , "UserName" , "PW" )
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die (mysql_error() );


//set the username post from login page
$Assessor=$_SESSION['Assessor'];
$AssessorID = $_SESSION['AssessorID'];
$now = date('F j, Y, g:i a', strtotime('+3 hours'));
$eid = @$_GET['emp_id'];

//gather data from employee list
$sql = "SELECT ID, emp_name, emp_id FROM $employee_table LEFT JOIN $check_table ON `ID` = `EmpID` ORDER BY ID ASC";
$result = mysql_query($sql) or die(mysql_error());

//begin building HTML table
echo '<form action="" method="get">';
echo '<table id="main" cellpadding="0" cellspacing="0">
   <tr id="header"><td id="info">';
if (isset($_SESSION['Assessor'])) {
echo '<p>Welcome '.$Assessor.' ('.$AssessorID.') ! <span class="date">Today is '.$now.'</span><br /><span class="instructions">Please choose an employee from the drop-down menu below </span>';}

elseif (!isset($_SESSION['Assessor'])) 
{
echo 'you are not logged in!';}

echo '<img src="novo-logo.png" alt="Novo Nordisk" /></p></td>';
echo '  </tr>
  <tr>
    <td>';

echo '<table id="employees">
  <tr>
    <td>
<select name="emp_id" onchange="this.form.submit()" />
    ';
while(list($id,$name,$emp_id) = mysql_fetch_row($result)){
// this will select the current employee int he dropdown box
$selected = $id == $eid ? "selected" : "";
    echo '<option value="'.$id.'" '.$selected.' />'.$name.' (' .$emp_id.')</option>'."\n";
$_SESSION['emp_name'] = $name;
$_SESSION['emp_id'] = $id;
}
echo '</select>
    </td>
  </tr>
</form>
</table>';
// Get checkboxes from emp_checks table
$ch = "SELECT Blocks FROM $check_table WHERE `EmpID` = '$eid'";
$cres = mysql_query($ch) or die(mysql_error());
$c = mysql_fetch_assoc($cres);
// put the comma seperated values in an array
$checks = explode(",", $c['Blocks']);

//Build and issue query
$sql="SELECT * from $grid_name ORDER BY Block";
$result = mysql_query($sql)or die(mysql_error());
// set the block groups
$lastblock = '';
echo '<form name="Grid" action="FU_Results.php" method="POST" />
<input type="hidden" name="emp_id" value="'.$eid.'" />

<table id="matrix" cellpadding="0" cellspacing="0">
  <tr>
    <th id="main" colspan="11"> Selling Skills Recommended Follow-up</th>
  </tr>';
while($row = mysql_fetch_assoc($result)){
// if the checkbox is in the array, check off the checkbox
$chk = in_array($row['ID'], $checks) ? "checked" : "";
  // checks to see if the block name has changed, if it has start a new row
  if($row['Block'] != $lastblock){
echo "<tr><td id='title'>".substr($row['Block'], 1)."</td>";
  }
  echo '<td class="checkbox"><input type="checkbox" name="grid[]" value="'.$row['ID'].'" class="inactive" '.$chk.' /></td><td class="text">'.$row['Text'].'</td>'."\n";
// set the lastblock to the current block in the loop
$lastblock = $row['Block'];
}
echo '</tr></table>
<div id="submit"><input type="image" src="Submit.png" name="submit" value="submit" alt="submit" class="SubmitButton" /></div>';
//end of HTML table
echo '</td>
  </tr>
</table>
</form>';
?>
</body>
</html>
<?
} else {
// redirect them to the login page
header('Location:AssessorLogIn.php');
}
?>

 

and the results display page

 


<?php
session_start();
if(($_SESSION['login'] == 1)  && isset($_SESSION['Assessor'])){
/*--------- DATABASE CONNECTION INFO--------- */
//set up table and database names
$db_name ="DBNAME";
$grid_name ="grid";
$employee_table="employees";
$check_table = "emp_checks";
//connect to server and select database
$connection = @mysql_connect( "localhost" , "UserName" , "PW" )
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die (mysql_error() );
//set the username post from login page
$Assessor=$_SESSION['Assessor'];
$Assessor_ID = $_SESSION['AssessorID'];
$emp = $_SESSION['emp_name'];
$emp_id = $_SESSION['emp_id'];
$now = date('Y-m-d h:i:s', strtotime('+3 hours'));

// set the employee id
$id = $_POST['emp_id'];
// change the array to a comma seperated string for storage
$blocks = implode(",", $_POST['grid']);
// check if employee has an entry in the emp_checks table
$check = "SELECT CheckID FROM $check_table WHERE EmpID = '$id'";
$cres = mysql_query($check) or die(mysql_error());
$found = mysql_num_rows($cres);
  if($found > 0){
  // if an employee was found  we update the check boxes
  $update = "UPDATE $check_table SET Blocks = '$blocks' WHERE `emp_id` = '$id'";
  mysql_query($update) or die(mysql_error());
  //echo $update;
  $message = 'Assessment results for '.$emp.',('.$emp_id.')  by '.$Assessor.',('.$Assessor_ID.') on '.$now;

  } else {
  // If employee was not found we insert the checkboxes
  
  $insert = "INSERT INTO $check_table (`Assessor`,`AssessorID`,`EmpName`,`EmpID`, `Blocks`,`date_uploaded`) VALUES ('$Assessor','$Assessor_ID','$emp','$emp_id', '$blocks', '$now')";
  mysql_query($insert) or die(mysql_error());
  //echo $insert;
  $message = 'Assessment results for '.$emp.',('.$emp_id.')  by '.$Assessor.',('.$Assessor_ID.') on '.$now;
  }
// run your code

echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Novo Nordisk Selling Skills Recommended Follow-up Results page</title>
<link rel="stylesheet" href="Global.css" type="text/css" media="screen, handheld" />
</head>
<body>
<table id="ResultsTable">
<tr>
<td colspan="2"><form action="index_test.php"><input type="image" src="Back.png" alt="Assess Another Employee" style="border:none"/></form>
<!--commented out for now
<img src="novo-logo.png" alt="Norvo Nordisk" />
-->
</td>
</tr>
<tr id="header"><th colspan="2" id="ResultsMessage">'.$message.' have been successfully submitted!</th></tr>
<tr>
<td align="center"> 
<table id="ResultsTableInner">';
$text = str_replace(",", "', '", $blocks);
$lastblock = "";
$sql = "SELECT * FROM `grid` WHERE `ID` IN ('$text') ORDER BY `Block`";
$res = mysql_query($sql) or die(mysql_error());
while($r = mysql_fetch_assoc($res)){
  if($r['Block'] != $lastblock){
  echo '
  <tr>
    <td colspan="2" id="block">'.substr($r['Block'], 1).'</td>
  </tr>';
  }
  echo '
  <tr>
    
    <td  id="results">'.$r['Text'].'</td>
  </tr>
  ';
$lastblock = $r['Block'];
}
echo '
</table>
</td>
</tr>
</table>
</body>
</html>
';
} else {
// redirect them to the login page
header('Location:AssessorLogIn.php');
}
?>


 

if it helps, I can provide a link to the application, to see what I am referring to. The vars for $Assessor, and $Assessor_ID are coming through fine, and I am pretty much trying to do the same thing for $emo, and $emp_id

 

Link to comment
Share on other sites

The second piece of posted code shows a blank line before the first <?php tag. If that is actually in the file, sessions won't start as that is content that will be output to the browser.

 

Add the following two lines after your first opening <?php tag to see what errors might be present -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

this is unrelated to my original issue but for the following code

<select name="emp_id" onchange="this.form.submit()" />
    ';
while(list($id,$name,$emp_id) = mysql_fetch_row($result)){
// this will select the current employee int the dropdown box
$selected = $id == $eid ? "selected" : "";
    echo '<option value="'.$id.'" '.$selected.' />'.$name.' (' .$emp_id.')</option>'."\n";
$_SESSION['emp_name'] = $name;
$_SESSION['emp_id'] = $id;
}
echo '</select>

 

I want to add a default option, instead of the first name being the default. The default should say 'choose an employee below'. How can I add that to my current code?

Link to comment
Share on other sites

Try this:

$selectOptions = '<option value="">choose an employee below</option>';

<select name="emp_id" onchange="this.form.submit()" />

while(list($id,$name,$emp_id) = mysql_fetch_row($result)){
    // this will select the current employee int the dropdown box
    $selected = $id == $eid ? "selected" : "";
    $selectOptions .= '<option value="'.$id.'" '.$selected.' />'.$name.' (' .$emp_id.')</option>'."\n";

    $_SESSION['emp_name'] = $name;
    $_SESSION['emp_id'] = $id;
}

echo $selectOptions;
echo '</select>';

 

I'd rather do this in my (X)HTML using a <label /> tag, but this will do what you asked for.

YMMV

Link to comment
Share on other sites

thank, I will give that a try.

 

I think this is currently a bit off. How can I format this part correctly?

 

echo '<table id="employees">
  <tr>
    <td>
<select name="emp_id" onchange="this.form.submit()" />

while(list($id,$name,$emp_id) = mysql_fetch_row($result)){
    // this will select the current employee int the dropdown box
    $selected = $id == $eid ? "selected" : "";
    $selectOptions .= '<option value="'.$id.'" '.$selected.' />'.$name.' (' .$emp_id.')</option>'."\n";

    $_SESSION['emp_name'] = $name;
    $_SESSION['emp_id'] = $id;
}

echo $selectOptions;
echo '</select>'
    </td>
  </tr>
</form>
</table>';

 

I have set this

 

$selectOptions = '<option value="">choose an employee below</option>';

 

at the top of the page with the rest of my variable declarations.

 

thanks!

 

 

Link to comment
Share on other sites

just an update on this. I have it displaying the defualt option set in a variable, but it displays not at the top as the default, but in the middle, if I have two other entries. I of course want the variable SelctOptions to display on top as the default value.

 

$selectOptions = "<option value=''>choose an employee below</option>";
    <td>
<select name="emp_id" onchange="this.form.submit()" />
    ';
while(list($id,$name,$emp_id) = mysql_fetch_row($result)){
// this will select the current employee in the dropdown box

$selected = $id == $eid ? "selected" : "";
echo ".$selectOptions.";
    echo '<option value="'.$id.'" '.$selected.' />'.$name.' (' .$emp_id.')</option>'."\n";
$_SESSION['emp_name'] = $name;
$_SESSION['emp_id'] = $emp_id;
}


echo '
</select>
    </td>

 

Link to comment
Share on other sites

I fixed the above problem, but the dropdown session doesn't cary over the right value of the employee to  my results page.

 

<select name="emp_id" onchange="this.form.submit()" />
    ';
while(list($id,$name,$emp_id) = mysql_fetch_row($result)){
// this will select the current employee in the dropdown box

$selected = $id == $eid ? "selected" : "";

    echo '<option value="'.$id.'" '.$selected.' />'.$name.' (' .$emp_id.')</option>'."\n";
$_SESSION['emp_name'] = $name;
$_SESSION['emp_id'] = $emp_id;
}


echo '
</select>

value stored on results page

 

$message = 'Assessment results for '.$emp.',('.$emp_id.')  by '.$Assessor.',('.$Assessor_ID.') on '.$now;

 

not coming through correctly. always shows the results being for the last name in my select value, and is also inserted into the database wrong, even though the actual checkbox results seem to be correct.

 

 

Link to comment
Share on other sites

thought I would go ahead and provide the link, so someone can actually see what I am referring to with my problem.

 

http://etsi-dataservices.com/test/AssessorLogIn.php

 

type in your first and last name, and Accessor ID, which should be four letters like ABCD. This isn't authenticating against anything so whatever you put in will take you to the assessment page. Choose an employee from the drop-down menu. and select as many of the squares or as little as you want. Click submit and you will be taken to a review page. This is where the problem is. The right results from the squares choses are coming through. but the employee information is not.

 

THANKS!

Link to comment
Share on other sites

just a note to my proble. I see what is happening and what is not happening.

 

The menu is selecting correctly. In my HTML menu I see

<td>
<select name="emp_id" onchange="this.form.submit()">
  <option value="">
Employee List
  </option>
  <option value="69">
Puff Daddy (PFDA)
  </option>
  <option value="70">
Biggie Small (BIGY)
  </option>
  <option value="71">
Tupac Shakur (TUPA)
  </option>
  <option value="72">
Lil Wayne (LILW)
  </option>
  <option value="73" selected="selected">
Nellie (NELI)
  </option>
  <option value="74">
Dr. Dre (DREY)
  </option>
</select>
</td>

 

after I select Nellie in the drop down menu.

 

however, the results still show the last entry in the menu, so for instance this displays 'Puff Daddy' instead of 'Nelly' here.

 

$message = 'Assessment results for '.$name.',('.$emp_id.')  by '.$Assessor.',('.$Assessor_ID.') on '.$now;

 

in my database I have two tables, on for employees and one for results. The values in the option field correspond correctly with the field 'ID' in my employee table. I have another table 'results' which should display the employee selected in the menu and the results of the form submission. Do I need to do a SQL JOIN in order for this to work correctly?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.