Here is what I am trying to do. I am using session variables and a drop down (select) menu so that the user can select from a list of values being stored in a MySQL DB. the user will then fill out a form with checkboxes relating to this employee they selected and when they submit they will be taken to a results page, which will display the information from the checkboxes they checked and also echo the employee name and ID. The problem I have specifically is the correct selection isn't carrying over to the results page. The last value in the select menu always displays no-matter what.
Here is a link to the application, so you can see what I am referring to:
http://etsi-dataservices.com/test/AssessorLogIn.php
you can type anything into the first and last name field. The ID field should be four letters Like AWOL. When you get to the form select one of the cartoon characters, and check a few boxes then submit. The wrong select value will be displayed.
code for the page with the select menu and form is
<?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" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(document.forms["Grid"].elements["grid[]"]).each(function()
{
$(this).click(function()
{
if($(this).hasClass("active"))
{
$(this).removeClass("active").addClass("inactive");
$(this).parent().next().andSelf().toggleClass("active");
}
else
{
$(this).removeClass("inactive").addClass("active");
$(this).parent().next().andSelf().toggleClass("active");
}
});
});
});
</script>
</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";
$selectOptions = "<option value=''>Employee List</option>";
//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 in the dropdown box
$selected = $id == $eid ? "selected" : "";
$selectOptions .= '<option value="'.$eid.'" '.$selected.' />'.$name.' (' .$emp_id.')</option>'."\n";
$_SESSION['emp_name'] = $name;
$_SESSION['emp_id'] = $emp_id;
}
echo ".$selectOptions.";
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 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'];
$name = $_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','$name','$emp_id', '$blocks', '$now')";
mysql_query($insert) or die(mysql_error());
//echo $insert;
$message = 'Assessment results for '.$name.',('.$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><form action="index_test.php"><input type="image" src="Back.png" alt="Assess Another Employee" style="border:none"/></form><td><a href="logout.php"><img src="LogOut.png" alt="Logout" style="border:none"/></a>
<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');
}
?>
any assistance would be greatly appreciated!