Jump to content

displaying information from select menu


webguync

Recommended Posts

I have another post out similar to this, but I wanted to start a new thread because I was misdiagnosing the problem earlier and I have it mostly resolved, just need a tiny bit more help.

 

 

I have a form select of different employees. Once you select the employee, there is a form with various checkboxes, you submit and are taken to a results page. All the checkbox information displays fine and is entered into the DB correctly, also the  employee ID that is selected is coming though O.K. The only part not coming though is employee name.

 

MySQL tables look like this:

 

employees

 

|ID|    |emp_name|      |emp_id|

92    Huckleberry Fin   HUCK

 

emp_checks

 

|CheckID| |EmpName| |EmpID|  |Blocks|        |Assessor| |AssessorID|    |date_uploaded|

  82                              92    6,8,12,15,20      Tom Jones  TOJO         2008-06-24 12:51:44

 

you will notice the field in the table emp_checks is blank. This is what I need to figure out how to get filled in and to display on my form results page.

 

here is the code in question

<?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'];
$name = @$_GET['emp_name'];
//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 '<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></p>';}

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


echo '  </tr>
  <tr>
    <td>';


echo '<table id="employees">
  <tr>
    <td>
<form action="" method="get">
<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="'.$id.'" '.$selected.' />'.$name.'  ('.$emp_id.')</option>'."\n";
$_SESSION['emp_name'] = $name;
$_SESSION['emp_id'] = $emp_id;
}
echo ".$selectOptions.";
echo '
</select>
  
</form>
</td>
</tr>

';
// 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>
</td>
</tr>
<tr>
<td>

<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>
</table>
  </td>
  </tr>
  </table>
';
?>

 

in the form results page, I am using:

 

<?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'];

//carry over session from select form
$name = $_SESSION['emp_name'];
$emp_id = $_SESSION['emp_id'];
//set the current date
$now = date('Y-m-d H:i:s', strtotime('+3 hours'));


// set the employee id
$emp_id = $_POST['emp_id'];

//set employee name
$name=$_POST['emp_name'];

// 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 `EmpID` = '$id'";
  mysql_query($update) or die(mysql_error());
  //echo $update;
  $message = 'Assessment results for '.$name.' 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 ('" . mysql_real_escape_string($Assessor) . "','" . mysql_real_escape_string($Assessor_ID) . "','" . mysql_real_escape_string($name) . "','" . mysql_real_escape_string($emp_id) . "', '" . mysql_real_escape_string($blocks) . "', '" . mysql_real_escape_string($now) . "')";
$rs = mysql_query($insert) or die("Problem with the query: $insert<br />" . mysql_error());


  
  
  //echo $insert;
  $message = 'Assessment results for '.$name.'  by '.$Assessor.',('.$Assessor_ID.') on '.$now;
  }
// run your code

echo '
<html>
<head></head>
<body>
<tr id="header"><th colspan="2" id="ResultsMessage">'.$message.' have been successfully submitted!</th></tr>
</body>
</html>
';

 

I have stripped out some of the code, but basically I am using the variable $name to display the name of the employee selected. Everything else is posting fine, but not the $name variable. Nothing displays in the message, and there is no DB entry.

 

Any help in the right direction is freatly appreciated. Let me know if I can provide more information to help resolve this issue.

 

 

the variable $name is what I am using to display the

Link to comment
https://forums.phpfreaks.com/topic/111724-displaying-information-from-select-menu/
Share on other sites

it looks like I can only retrieve one value from my select menu. I have tried and haven't been successfull. My question now becomes this. If I am able to retrieve the correct selected value of an ID

 

eg

 

<select>

<option value="4">Tom Jones</option>

</select>

 

what would be the best way to retrieve the information of an associative field. So in my database table I have three fields one is ID, (the value being retrieved), the other is emp_name, and the third is emp_id. So basically I know that ID of 4 = Tom Jones, so what would I need to do to gather that information from the MySQL db?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.