?
Solved

Populating a drop down box with values from a database

Posted on 2003-03-24
12
Medium Priority
?
194 Views
Last Modified: 2010-04-01
Okay so I can create a delightful little recordset full of the values I want for my drop down box but how do I get the little buggers IN the drop down box? Code examples would help me best. Thanks!
0
Comment
Question by:Redbirdofthesouth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
12 Comments
 
LVL 3

Accepted Solution

by:
allahabad earned 500 total points
ID: 8199523
<table > 
   
     
    <tr>Values </td>
         
       <td><select name="ListOfValues" maxlength="10">
                     
              <%
                  // result is the ResultSet retrived from DB
                  while (result.next()){
                     
                     // display value of the first column in the SELECT statement
                     String valueFromDB = result.getString(1);
              %>          
                     <option value="<%=valueFromDB %>">"<%=valueFromDB %>"
                  }
         </select>
         <%
                  result.close();
         %>
        <td>      
   </tr>
   
</table>
   
0
 

Expert Comment

by:arisadrian
ID: 8200128
<script>
var selectedValue = dropdownName{dropdownName.selectedIndex].value;

alert("value of selectedValue is :  " + selectedValue);
</script>

I think this is what you need...
0
 

Expert Comment

by:arisadrian
ID: 8200132
just a little correction...  sorry...

<script>
var selectedValue = dropdownName[dropdownName.selectedIndex].value;

alert("value of selectedValue is :  " + selectedValue);
</script>

I think this is what you need...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:arisadrian
ID: 8200519
however, if you are using a <form> tag, you just need to consider the form name of your drop down...

<script>

function functionName()
{
    var selectedValue = document.formName.dropdownName[document.formName.dropdownName.selectedIndex].value;

    alert("value of selectedValue is :  " + selectedValue);

}

</script>

good luck...
0
 
LVL 14

Assisted Solution

by:kennethxu
kennethxu earned 500 total points
ID: 8203083
I agree with allahabad, just some code need to be fixed inside <td></td>

             <%
                 // result is the ResultSet retrived from DB
                 while (result.next()){
                   
                    // display value of the first column in the SELECT statement
                    String valueFromDB = result.getString(1);
             %>          
                    <option value="<%=valueFromDB %>">"<%=valueFromDB %>"
        <%
                 }
                 result.close();
        %>
        </select>
0
 

Author Comment

by:Redbirdofthesouth
ID: 8203215
Thanks, you guys are great! I can't wait to put it into practice.
I'll let you know if I have any other issues.
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8204798
Yes, kennethxu . I missed starting tag <td> in first cell, and in the next cell missed closing tag </td> .

Here is the revised one.

<table > 
             
                 
               <tr><td>Values </td>
                     
                  <td><select name="ListOfValues" maxlength="10">
                                 
                         <%
                             // result is the ResultSet retrived from DB
                             while (result.next()){
                               
                                // display value of the first column in the SELECT statement
                                String valueFromDB = result.getString(1);
                         %>          
                                <option value="<%=valueFromDB %>">"<%=valueFromDB %>"
                             }
                    </select>
                    <%
                             result.close();
                    %>
                   </td>      
              </tr>
             
  </table>
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8206704
you should also put "}" in scriptlet.
0
 

Author Comment

by:Redbirdofthesouth
ID: 8373225
I have searched and searched and I can't find the second part to this: What if I need to have multiple values in the select values: Like each item is Last Name, First Name.

And then, what if I want to include a value like Name_ID and have it be invisible?

Any ideas?
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8374153
<option value="<%=nameID %>"><%=firstName%> <%=lastName%></option>
0
 
LVL 35

Expert Comment

by:girionis
ID: 10060819
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between allahabad and kennethxu

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question