提交带有多个下拉框的表单?

时间:2014-03-24 09:07:03

标签: java javascript forms servlets jdbc

在下面的代码中,我在单个表单中有多个下拉框。在下拉框中,我显示默认值。 但是当用户登录时,用户可以更改下拉列表中的值并单击“提交”按钮。

<form method="post" id="myForm" name="myForm">

                                 <%            
                                            while(rs1.next())
                                                {

                                  %>            

                                                 <table id="main_table" style="width: 600px; border: 0px solid #ccc;  padding: 1px; border-spacing: 1px;  ">


                                                 <tbody>
                                                 <tr class="ab" style="background-color:#3f92aa; color:white; width:600px; ">
                                                 <td colspan="2" class="flip" style="width:125px; font-size:14px; text-align:left; font-family:Palatino Linotype;"><%=rs1.getString(3) %><input type="hidden" name="statename" value="Goa"></td>
                                                 <td colspan="2" class="flip"  style="width:125px; font-size:14px; text-align:center; font-family:Palatino Linotype;"><%=rs1.getString(4) %></td>
                                                 <td colspan="2" class="flip"  style="background-color:#cb3315; width:125px; font-size:14px; text-align:center; font-family:Palatino Linotype;"><%=rs1.getString(5) %></td>


                                                 <td colspan="2" style="width:125px; font-size:13px; font-family:Palatino Linotype; text-align:center;">

                                                  <table style="border: 0px;border-spacing: 0px;padding: 0px;">
                                                   <tr>

                                                                      <td>&nbsp;</td>
                                                                              <td>
                                                                                      <select name="<%=rs1.getString(3) %>">
                                                                                              <option value="<%=rs1.getString(5) %>"><%=rs1.getString(5) %></option>
                                                                               <%
                                                                                Statement stmt2=null;
                                                                                DBconnection db2=new DBconnection();
                                                                                Connection con2=db2.dbConn();
                                                                                try{
                                                                                stmt2 = con2.createStatement();   
                                                                                         ResultSet rs2 = (ResultSet) stmt2.executeQuery("select distinct WinnerParty from election_history;");
                                                                                         while(rs2.next())
                                                                                         {
                                                                                 %>

                                                                                                 <option value="<%=rs2.getString(1) %>"><%=rs2.getString(1) %></option>

                                                                                <%

                                                                                          }
                                                                                     }
                                                                                     catch(SQLException e){
                                                                                     e.printStackTrace();
                                                                                     }
                                                                                     finally{
                                                                                     con2.close();
                                                                                     stmt2.close();
                                                                                      }

                                                                                 %> 

                                                                                      </select>
                                                                                      </td>
                                                                                      </tr>
                                                                                      </table>



                                                                   </td>
                                                                   </tr>
                                                                   </tbody>



                                     </tbody>



                                      </table>
                                      </form> 

Servlet代码:

con=DBconnection.dbConn();
                 stmt=con.createStatement();


                 String statenames=request.getParameter("statename");
                 System.out.println("this is State name ==="+statenames);

                ResultSet rs = (ResultSet) stmt.executeQuery("select distinct Constituency from election_history where State='"+statenames+"'");

                //rs.next();
                while(rs.next()){
                         constituencies.add(rs.getString(1)); 


                }
                 System.out.println("list of constituencies are=="+constituencies);

                 for(int i=0;i<constituencies.size();i++)
                {
                    String Cname=constituencies.get(i);
                    System.out.println("Cname value is=="+Cname);  
                    String Cselected=request.getParameter(Cname);
                    System.out.println("Cselected value is=="+Cselected);
                    map.put(Cname, Cselected);
                }

输出:

inside editYPName...
Connected to the database
this is State name ===Goa
list of constituencies are==[Panaji, Mormugao]
Cname value is==Panaji
Cselected value is==null
Cname value is==Mormugao
Cselected value is==INC

但是在这里我的问题是当我提交一个表单时,我在servlet中只有一个值(CSelected)下拉列表,因为你可以看到输出内部和剩余值为null。 谁能告诉我这里的问题是什么?

1 个答案:

答案 0 :(得分:0)

如果您有多个名为statename的下拉列表,请不要使用 String statenames = request.getParameter("statename");而是使用:

String[] statenames = request.getParameterValues("statename");