Log in

View Full Version : JSP - Dynamic Combo box using SQL


robtyketto
Feb 24, 2008, 05:21 PM
Greetings,

I'm a beginner to jsp and from example code I have started to write the beginnings of a simple FAQ system, which allows the user to choose a question from a list of categories. Using combo boxes.

I would like the user to select a Category and then a Question, upon which it then displays the information onscreen in an HTML table.

Im at the point where they are populated and work statically, but I can't understand the onchange property of the select.

I need the value in the first box to determine the value in the second combo box.

Ideally I would like to avoid using arrays and to keep things simple as its for a university course and they don't want a complex solution (they don't expect you to have a great deal of java knowledge).

Here is my code so far, any advice and suggestions would greatly be appreciated.
:)


<SELECT NAME="Category" id= Category onChange="location.href='wk465682UserMenu.jsp?.option='+this .value;">

Will the code above really pass in the value they have selected when reloading the jsp, it doesn't appear too.

Thanks
Rob


<!-- the % tag below is what is called a scriptlet tag - allows java to be embedded in the jsp -->
<%@ page import="java.util.*" %>
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<HTML>
<H1>FAQ</H1>

<H3>Category choice</H3>
<FORM ACTION="wk465682UserMenu.jsp" METHOD="POST">
<%
String CategoryCombo = null;%>
<SELECT NAME="Category" id= Category onChange="location.href='wk465682UserMenu.jsp?.option='+this .value;">
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:odbc:FAQ");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT DISTINCT CATEGORY FROM FAQ" );%>
<OPTION VALUE='nochoice'>Please choose a category</OPTION>";
<%while(rs.next()) {
CategoryCombo = rs.getString("Category");%>
<OPTION><%out.println(CategoryCombo);%>
</OPTION>
<% } %>
</SELECT>
<BR><BR>
<H3>Question selection</H3>
<%
String QuestionCombo = null;%>
<SELECT NAME="Question" id = Question>
<OPTION VALUE='nochoice'>Please choose a question</OPTION>";
<%ResultSet ss = statement.executeQuery("SELECT * FROM FAQ WHERE CATEGORY = ( '" + CategoryCombo + "')");
while(ss.next()) {
QuestionCombo = ss.getString("Question");%>
<OPTION><%out.println(QuestionCombo);%>
</OPTION>
<% }
//response.sendRedirect("wk465682UserMenu.jsp");
//
%>
</SELECT>
</FORM>
</HTML>