View Full Version : Select - onchange submit form

Jul 27th, 2012, 01:54 AM

I need some help with this function.

I have a table with data:
-------- ------- ---------
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 need to select this continue form without selecting a submit button. So, i figure I could use the onchange : this.formname.submit(), might help. 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.empselect.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']?>">

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

However, it is not doing the echo part. Am I making a mistake somewhere. I need to get the grade for further processing on the form, and I needed it from the select.

Your kind assistance is greatly appreciated.

Jul 27th, 2012, 02:54 AM
Consider that the browser might be set to not allow such a function, since it might be used to fool people into putting malicious code on their machine.

Jul 27th, 2012, 03:00 AM
I don't think that might be the case as it doesn't work in any of my browsers. I don't know if what I have written is completely correct either. Do I need some additional Javascript code too?

Note that the grade was retrieved when the employees details were selected. The list shows both the name and grade, separated by a hyphen. However as the value associated with the employee was the name, I also wanted the grade. I have to further process this script based on teh grade. That is the reason i created a hidden input for the grade.

Old Pedant
Jul 27th, 2012, 03:06 AM
<select name="employee" id="employee" class="styled-select" onchange="this.form.submit()">

There was no such thing as


Consider: this refers to the <select>. So this.empselect would be looking for a property named empselect *OF* the <select>! Of course none such exists.

But this.form refers to the enclosing <form> of any form element.

Jul 27th, 2012, 10:26 AM
Thanks for your replies. I initially did try it with this.form.submit. The problem with that is the value in the select drop down does not get retained. It jumps back to the default element immediately and only the last value of grade is made available to $grade. Any more ideas ??

Jul 27th, 2012, 11:53 AM
Solution has been found...Thanks for help...