• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

Populating a drop down box with values from a database

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
Redbirdofthesouth
Asked:
Redbirdofthesouth
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
allahabadCommented:
<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
 
arisadrianCommented:
<script>
var selectedValue = dropdownName{dropdownName.selectedIndex].value;

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

I think this is what you need...
0
 
arisadrianCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
arisadrianCommented:
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
 
kennethxuCommented:
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
 
RedbirdofthesouthAuthor Commented:
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
 
allahabadCommented:
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
 
kennethxuCommented:
you should also put "}" in scriptlet.
0
 
RedbirdofthesouthAuthor Commented:
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
 
kennethxuCommented:
<option value="<%=nameID %>"><%=firstName%> <%=lastName%></option>
0
 
girionisCommented:
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

Technology Partners: 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!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now