-2

I have multiSelect drop down on my web page designed in JSP. End user will select required options and save the form to Database. Below is the original Dropdown that gets values and index from Database Master Table. Selected values are saved in another table.

Original Dropdown on the page.

 <table>
 <tr>
 <td> Departments Impacted   </td>
    <td><select multiple="multiple" name="abc" id="abc">
     <%

         Statement stt3 = conn.createStatement();
         ResultSet rstt3 = stt3.executeQuery("SELECT DEPT_DEPARTMENT_ID, DEPT_NAME FROM [PA].[dbo].[PA_DEPT_DEPARTMENT] ORDER BY DEPT_NAME ASC");
         while (rstt3.next()) {

         %>

         <option value="<%=rstt3.getString(1)%>"><%=rstt3.getString(2)%>
         </option>
         <%
             }
            stt3.close();
     rstt3.close();
                            %>
    </select></td>  </tr>

Below code retrieve saved database values and display them as selected in new select tag. However, I want the selected values to be show in above table instead of new Dropdown.

<%
 ArrayList al = new ArrayList();    
 String dept = null;
 String deptID = "SELECT DEPARTMENT_ID FROM  [PA_RD_DEP_REQ_DETAILS_DEPARTMENT_MAP] inner join  [PA_RD_REQUISITION_DETAILS] on RD_DEP_RD_ID=RD_ID where RD_ID=?";

         PreparedStatement pst4 = conn.prepareStatement(deptID);
         pst4.setString(1, RD_ID);
         ResultSet rstt1 = pst4.executeQuery();
         while (rstt1.next()) {
             dept=rstt1.getString(1);
            al.add(dept);

         }
         for(int i=0; i<al.size(); i++)
         {
         String deptName = "SELECT [DEPT_NAME] FROM  [PA_DEPT_DEPARTMENT] WHERE [DEPT_DEPARTMENT_ID]=?";
         PreparedStatement pst5 = conn.prepareStatement(deptName);
         pst5.setString(1, (String)al.get(i));
         ResultSet rstt5 = pst5.executeQuery();
         while (rstt5.next()) {
             %>
             <option value="<%=(String)al.get(i)%>" selected="selected"><%=rstt5.getString(1)%>
             <%  


         }
         }


         %>
naveen kumar
  • 41
  • 1
  • 6
  • 10

1 Answers1

0

Adding the selected attribute, to an option tag will make it selected. This can be done for multiple option tags, without the need for JavaScript:

<select multiple="multiple">
  <option selected="selected" value="volvo">Volvo</option>
  <option selected="selected" value="saab">Saab</option>
  <option value="vw">VW</option>
  <option value="audi">Audi</option>
</select>
David Pärsson
  • 6,038
  • 2
  • 37
  • 52
  • David, thanks for the answer. However, I want to make OPTION selected, if the user selects it and saves it to database. So have selected values in database. If the user opens the form again, then I need to make the OPTIONS selected for which values are stored in DB. – naveen kumar Nov 06 '12 at 21:04
  • That should ideally be done as described above when the HTML code is generated. – David Pärsson Nov 06 '12 at 21:06
  • I can help you solve _specific_ problems, but I won't write your program. – David Pärsson Nov 06 '12 at 21:12
  • That's what I want, I don't want program. I prefer program writing done by me. Help me with resolving this problem. Write two lines what Actually I need to do. Thank you. – naveen kumar Nov 06 '12 at 21:15
  • I have updated the code that I tried. I want to show the selected in dropdown available in the table. – naveen kumar Nov 07 '12 at 04:22