Welcome Guest, Not a member yet? Register   Sign In
How to retrieve employee id in a input box based on employee name drop down from database ??
#1

[eluser]rainfall[/eluser]
I fetched data ( employee name ) on drop down list from database . Now I want to show its corresponding employee id in a input box below . Need to show the corresponding Id in input box when i change selection.
Its not working !!!!
Need help ...

J query in view
Code:
[removed][removed]
[removed]
$(document).ready( function() {
    $('#empName').change( function() {
       $('#empId').val( $(this).val() );
    });
});
[removed]




<tr>
                <td><b> Employee Name: </b></td>
                <td><select name="level" id="empName">
                       &lt;?php

            foreach($groups as $row)
            {
              echo '<option value="'.$row->employee_name.'">'.$row->employee_name.'</option>';
            } ?&gt;
                    </select>
                </td>
            </tr>
            <tr>
                <td><b>emp_id:</b></td>
                <td>&lt;input  type="text" id="empId" value="" /&gt;&lt;/td>
            </tr>

Code in the model
Code:
$query = $this->db->query("SELECT emp_id,employee_name FROM tbl_sample ORDER BY employee_name");
      {
         while($row = mysql_fetch_assoc($query)) {
    
       $groups[$row['emp_id']] = $row['employee_name'];
       }
     }
        return $query->result();
#2

[eluser]Flemming[/eluser]
"It's not working" is not enough info for anyone to be able to help you!!!

What is happening? Anything? Any JS error?

I would do a console.log to see what (if any) value is stored in $(this).val

Code:
$(document).ready( function() {
    $('#empName').change( function() {
    console.log('selected: ' + $(this).val();
       $('#empId').val( $(this).val() );
    });
});

and look in developer tools (Chrome) or Firebug (Firefox) at the console to see what's happening when you change the select.

hope that helps?
#3

[eluser]rainfall[/eluser]
Data fetched in dropdown ... but not showing the Id of employee in input box according to data.
#4

[eluser]ivantcholakov[/eluser]
@rainfall

1. Your code in the model is strange. Why do you iterate the query result and then you just return it? What is $groups array there for?

2. It would be better if you populate your dropbox options this way and then to adapt other code accordingly:

Code:
// Employees is an array.
            foreach($employees as $emp_id => $employee_name)
            {
              echo '<option value="'.$emp_id.'">'.$employee_name.'</option>';
            }

The idea is your dropbox to select employee ID directly.

3. Check what would happen if the query does not return anything. Also, in your dropbox maybe you need a default option at the beginning:

Code:
echo '<option value="">-- Choose an Employee --</option>';

Deal with the case when there is no Employee selected.
#5

[eluser]rainfall[/eluser]
I need to show the emp id in another input box when i select name from list ..
#6

[eluser]Flemming[/eluser]
hmmmm... this reminds me of the video of the bear who NEEDS an iPhone!

ivantcholakov's advice is sound.

And you need to see what value is returned on change of the select - you can alert it if you're not comfortable using console.log - but you do need to know what is happening on change somehow so that you can begin to debug this.

What do you get if you change your JS to this?

Code:
$(document).ready( function() {
    $('#empName').change( function() {
    alert('selected: ' + $(this).val());
       $('#empId').val( $(this).val() );
    });
});

?
#7

[eluser]rainfall[/eluser]

Debug in console shows this :


Code:
Failed to load resource: the server responded with a status of 404 (Not Found)
Uncaught ReferenceError: $ is not defined
#8

[eluser]Flemming[/eluser]
Looks like jQuery is not being loaded! Did you remember to include it before your
Code:
$(document).ready(function(){...
?
#9

[eluser]rainfall[/eluser]
i put

type="text/javascript" src="jquery-1.4.2.min.js in script tag



there is jquery-1.4.2.min in my project folder
#10

[eluser]Flemming[/eluser]
Well that 404 message is telling you that your jQuery file isn't getting loaded! You need to check the path to your jquery file! :-)




Theme © iAndrew 2016 - Forum software by © MyBB