Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-27-2012, 10:34 AM   PM User | #1
RCR
New Coder

 
Join Date: Jul 2012
Posts: 15
Thanks: 7
Thanked 0 Times in 0 Posts
RCR is an unknown quantity at this point
select - onchange submit form

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.
RCR is offline   Reply With Quote
Old 07-27-2012, 11:46 AM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 Posts
Arcticwarrio is on a distinguished road
it would be easier to query again later on to get the grade from the EMPNO
Arcticwarrio is offline   Reply With Quote
Users who have thanked Arcticwarrio for this post:
RCR (07-27-2012)
Old 07-27-2012, 11:54 AM   PM User | #3
RCR
New Coder

 
Join Date: Jul 2012
Posts: 15
Thanks: 7
Thanked 0 Times in 0 Posts
RCR is an unknown quantity at this point
I found an alternate way to get the grade...thanks for your help...
RCR is offline   Reply With Quote
Old 07-27-2012, 11:54 AM   PM User | #4
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 Posts
Arcticwarrio is on a distinguished road
or:

PHP Code:
<!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>
Arcticwarrio is offline   Reply With Quote
Users who have thanked Arcticwarrio for this post:
RCR (07-29-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:06 PM.


Advertisement
Log in to turn off these ads.