balaganesh.k89 Posted January 17, 2009 Share Posted January 17, 2009 Hi guys, I've displayed the company ids in a drop down box. If a user selects a id the name gotta appear in the another text box which is in the same page. Both id \s and names re stored in database. Can anyone help me out? ??? Thanks in advance. Bala the Billa Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted January 17, 2009 Share Posted January 17, 2009 do mean you mean a simple select element? <select> <option value="1">a name</select> <option value="2">another name</select> </select> you can simply put the id's in the values and the names for display Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2009 Share Posted January 17, 2009 The solutino will depend upon several factors. If you need to have it show dynamically (i.e. the user does not have to submit the page to get the value) then it will require JavaScript. You could either do it totally within JavaScript by doing a DB query of all the ID/name pairs and creating JavaScript variables or you could do an AJAX request each time the user changes the select list. The first option is typically much faster, but if you have many, many records it might be a problem. AJAX would require a hit to the database each time the user changes the select list, but only for the specific record. It can also have a slight delay. One very easy way to do it in this situation would be for the select field to have the ID as the label for the options and the name as the value for the options. That field would pass the name with the form - but you could also use the value (the name) to populate a text field. But, you would not have the ID in any field - so just populate a hidden field with the label of the option selected. <html> <head> <script> function populateUserName(selObj) { var selOption = selObj.options[selObj.selectedIndex]; if (selOption.value) { document.getElementById('user_name').value = selOption.value; document.getElementById('user_id').value = selOption.text; } else { document.getElementById('user_name').value = ''; document.getElementById('user_id').value = ''; } } </script> </head> <body> User ID:<br> <select name="user_sel" onchange="populateUserName(this);"> <option value="">--Select--</option> <option value="Bob Smith">4</option> <option value="Thomas Edison">12</option> <option value="Wilber Johnson">27</option> </select> <br></b><br> User Name:<br> <input type="text" name="user_name" id="user_name" /> <br></b><br> User ID [Make this hidden]:<br> <input type="text" name="user_id" id="user_id" /> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.