...

View Full Version : select - onchange submit form



RCR
07-27-2012, 11:34 AM
Hello,

I need some help with this function.

I have a table with data:
EMPNO ENAME GRADE
-------- ------- ---------
100 JACK A
101 JOHN B
103 TOM A

I need to create a select item that will list all the names. I want to be able to select one of the names from the drop down. I want to create a form without selecting a submit button. So, i figured I could use the onchange : this.formname.submit(). However I can't seem to understand where I am making a mistake. This is the code I've written.

<form name="empselect" action="?emp_select" method="post">
<h3>Select the Employees Name:
<select name="employee" id="employee" class="styled-select" onchange="this.form.submit()">
<option value="">Select an employee </option>
<?php foreach ($employees as $employee):?>
<option value="<?php echo($employee['empno']);?>">
<?php echo($employee['ename'].'-'.$employee['grade']);?></option>
<?php endforeach;?>
</select> </h3> <br>
<input type="hidden" name="grade" value="<?php echo $employee['grade']?>">
</form>

In the php part that was written above the form, I wrote:
if (isset($_GET['emp_select']))
{
$grade=$_POST['grade'];
echo "grade is" . $grade;
}

I am making a mistake somewhere. I need to get the grade for further processing on the form, and I needed it based on user selection of the employee's name. At this time, the only the grade of the last employee is available to $grade, also the value that i selected from the drop down immediately jumps back to the default.

Just a further explanation of my code:
1. A query retrieved the empno, name and grade.
2. The select is displaying a combination of the employeename-grade as in:
JACK-A
3. I want to select a name, and have the drop down return the employee's ID, but a hidden element submit the grade.
4. I need to program another form later in the document using the $grade value, based on the employee that I selected.
5. I do not want a separate submit button attached to the selection of the employee.

Your kind assistance is greatly appreciated.

Arcticwarrio
07-27-2012, 12:46 PM
it would be easier to query again later on to get the grade from the EMPNO

RCR
07-27-2012, 12:54 PM
I found an alternate way to get the grade...thanks for your help...

Arcticwarrio
07-27-2012, 12:54 PM
or:


<!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>Untitled Document</title>
</head>

<body>

<?php $Result = Q("SELECT * FROM table"); ?>
<form name="empselect" action="" method="post">
<h3>Select the Employees Name:
<select name="employee" id="employee" class="styled-select" onchange="this.form.submit()">
<option value="">Select an employee </option>
<?php
while ($employee = mysql_fetch_array($Result)){
echo '<option value="'.$employee['empno'].'">'.$employee['ename'].'-'.$employee['grade'].'</option>';
}
?>
</select> </h3> <br>
</form>

In the php part that was written above the form, I wrote:
<?php
if (isset($_GET['emp_select'])){
$Result = Q("SELECT * FROM `tablename` WHERE `empno` = ".$_GET['emp_select']);
$Row = mysql_fetch_array($Result);
echo "grade is " .$Row['grade'];
}
?>


</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum