Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    RCR
    RCR is offline
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    it would be easier to query again later on to get the grade from the EMPNO

  • Users who have thanked Arcticwarrio for this post:

    RCR (07-27-2012)

  • #3
    RCR
    RCR is offline
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I found an alternate way to get the grade...thanks for your help...

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    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>

  • Users who have thanked Arcticwarrio for this post:

    RCR (07-29-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •