How can I pass selected value from combobox to SQL statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rania Hassan
    New Member
    • Feb 2011
    • 2

    How can I pass selected value from combobox to SQL statement?

    I created JSP page containing a form with two comboboxes, how can I get the selected value from the first combobox to pass it to SQL statement used by the second combobox?

    here is my code with notes:
    Code:
    <%@ page language="java" contentType="text/html; charset=windows-1256"
        pageEncoding="windows-1256"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <%@ page  import="java.io.IOException"%>
    <%@ page import="java.sql.Connection"%>
    <%@ page import="java.sql.DriverManager"%>
    <%@ page import="java.sql.PreparedStatement"%>
    <%@ page import="java.sql.ResultSet"%>
    
    <%@ page import="javax.servlet.http.*" %>
    <%@ page import="javax.servlet.http.HttpServletRequest" %>
    <%@ page import="javax.servlet.http.HttpServletResponse" %>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
    
    </head>
    <body>
    	<%!
    		private Connection connect = null;
    		private PreparedStatement pstmnt;
    		private String sql;
                    private String sql1;
    		private ResultSet rs;
    		private String cc= "";
    		String cat_e="";
    							%>	
    		
    		<div align="center"><b>Subscription Form</b></div><br/>
    	<form name="subfrm" align="center" action="SaveSubscription"> 
     		Event Category: <select name="evnt_cat"> 
     									<option value="0">--Select Category--</option>
     		<%
    							try {
    					               connect = DriverManager.getConnection("jdbc:odbc:MyEvent","","");
    					               sql = "SELECT * FROM Event";
    					               pstmnt = connect.prepareStatement(sql);
    					               rs = pstmnt.executeQuery();
    					               while(rs.next()) {
    					               	cat_e = rs.getString("Event_Category");
    							%>
    					            	   <option value="<%=cat_e%>"><%=cat_e%></option>
    					         <%      } 
    
    					               rs.close();
    					               pstmnt.close();
    					               connect.close();
    					             }
    					         catch( Exception e ) {
    					           System.out.println(e);
    					           
    					         }
    							%>
    						</select><br/><br/>
    						
    		Event Sub_Category: <select name="evnt_subcat">
    									<option value="0">--Select Subcategory--</option>
    							<%
    							try {
    					               connect = DriverManager.getConnection("jdbc:odbc:MyEvent","","");
    					               sql1 = "SELECT EventDetails.Event_Title FROM Event INNER JOIN EventDetails ON Event.Event_ID = EventDetails.Event_ID WHERE Event.Event_Category = ?";
    					               pstmnt = connect.prepareStatement(sql1);
    					               pstmnt.setString(1,"The selected value from the 1st combobox");<--- Note: here, I need to put the value selected from the first combobox.
    					               rs = pstmnt.executeQuery();
    					               while(rs.next()) {
    					               	cc = rs.getString("Event_Title");
    							%>
    					            	   <option value="<%=cc%>"><%=cc%></option>
    					         <%      } 
    
    					               rs.close();
    					               pstmnt.close();
    					               connect.close();
    					             }
    					         catch( Exception e ) {
    					           System.out.println(e);
    					           
    					         }
    							%>
    						</select><br/><br/>
     		<input name="subusr" value="Subscribe" type="submit" align="center"/>
     	</form>
    </body>
    </html>
    Last edited by acoder; Feb 15 '11, 09:14 AM. Reason: Added [code] tags - please use them
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    There are a number of ways. How do you want the interaction to take place? Do you want the 2nd combo box to be changed after submit with a page reload, or does it have to be instant?

    Comment

    • Rania Hassan
      New Member
      • Feb 2011
      • 2

      #3
      Well, I need to do the following:
      once I select value from the first combobox it should be passed to the SQL used by the second combobox to be filled.

      Can this be done before pressing submit?

      Thanks for your reply :)

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Yes, it can, but you will need to use Ajax. If you're not familiar with that, read up on a basic tutorial or two - see the sticky thread at the top of this forum.

        Comment

        Working...