Jump to content

Need Help in Drop down box


balaganesh.k89

Need Help in Drop down box ?  

1 member has voted

  1. 1. Need Help in Drop down box ?

    • accessing database
      0
    • mysql
      1


Recommended Posts

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.