473,513 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JSP Beginner : Dynamic ComboBox

108 New Member
Greetings,

Im a newbie to JSP/Javascript (unfortunalley).

However I found enough example to create code that connects to my access db and populates two combo boxes.

The selection of the first box is used to populate the second (cascading).
See code below, apologies for the formatting as I wrote it in notepad.

I am assuming the code runs through once and therefor combo box populated only once.

How can I make them dynamic, needing some how to perhaps include a loop. reload form or use an onchange even (I need some advice and guidance with these plase)?

Preferrably without the use of arrays and with a simple structure as below, so I can understand it.

Any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. <!-- the % tag below is what is called a scriptlet tag - allows java to be embedded in the jsp -->
  2. <%@ page import="java.util.*" %>
  3. <%@ page language="java" %>
  4. <%@ page import="java.sql.*" %>
  5. <HTML>
  6. <H1>FAQ</H1>
  7.  
  8. <H3>Please choose a category</H3>
  9. <FORM ACTION="wk465682UserMenu.jsp" METHOD="POST">
  10. <%
  11. String CategoryCombo = null;%>
  12. <SELECT NAME="Category" id = Category>
  13. <%
  14. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  15. Connection conn = DriverManager.getConnection("jdbc:odbc:FAQ");
  16. Statement statement = conn.createStatement();
  17. ResultSet rs = statement.executeQuery("SELECT DISTINCT CATEGORY FROM FAQ" );
  18. while(rs.next()) {
  19. CategoryCombo = rs.getString("Category");%> 
  20. <OPTION><%out.println(CategoryCombo);%>
  21. </OPTION>
  22. <% } %>
  23. </SELECT>
  24. <BR><BR>
  25. <H3>Please choose a question</H3>
  26. <%
  27. String QuestionCombo = null;%>
  28. <SELECT NAME="Question" id = Question>
  29. <%ResultSet ss = statement.executeQuery("SELECT * FROM FAQ WHERE CATEGORY = ( '" + CategoryCombo + "')");
  30. while(ss.next()) {
  31. QuestionCombo = ss.getString("Question");%> 
  32. <OPTION><%out.println(QuestionCombo);%>
  33. </OPTION>
  34. <% }
  35. //response.sendRedirect("wk465682UserMenu.jsp");
  36. //
  37. %>
  38. </SELECT>
  39. </FORM>
  40. </HTML>

Thanks and hope this makes sense
Rob
Feb 21 '08 #1
6 9394
robtyketto
108 New Member
Im looking into the onchange function now, is this the right direction to go into?
Feb 22 '08 #2
acoder
16,027 Recognized Expert Moderator MVP
You said you want to avoid arrays. In that case, you have two options. Either refresh the page onchange with the new options, or use Ajax to get the new options to populate the second combo box.
Feb 22 '08 #3
robtyketto
108 New Member
Can you expand on how to refresh the page onchange with the new options please?

Cheers
Rob
Feb 22 '08 #4
acoder
16,027 Recognized Expert Moderator MVP
Change the location.href to point to the same page but with the option, e.g. onchange="location.href='index.jsp?opt='+this.valu e". Then you could check for opt using JSP and use that to populate the combo box.
Feb 22 '08 #5
robtyketto
108 New Member
Thanks for your help (Apologies if my response seems slightly retarted).
Im also googling other example to get a better understand of whats goin on.

I modified my code (see below)
When the value in the dropdown box is changed it reload the jsp and I expected the url to include the value of the option selected:-

This
http://localhost:8080/examples/wk465682UserMenu.jsp?.option=FTP
(Value of combo box)

Not this
http://localhost:8080/examples/wk465682UserMenu.jsp?.option=

I Shouldnt expect this to auto populate the option value on reload should I?
As I believe the SQL will retrieve the values in the combo box and ignore its last value before change.

Expand|Select|Wrap|Line Numbers
  1. <SELECT NAME="Category" id= Category onChange="location.href='wk465682UserMenu.jsp?.option='+this.value;">
  2. <%
  3. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  4. Connection conn = DriverManager.getConnection("jdbc:odbc:FAQ");
  5. Statement statement = conn.createStatement();
  6. ResultSet rs = statement.executeQuery("SELECT DISTINCT CATEGORY FROM FAQ" );%>
  7. <OPTION VALUE='nochoice'>Please choose a category</OPTION>";
  8. <%while(rs.next()) {
  9. CategoryCombo = rs.getString("Category");%> 
  10. <OPTION><%out.println(CategoryCombo);%>
  11. </OPTION>
  12. <% } %>
Feb 22 '08 #6
acoder
16,027 Recognized Expert Moderator MVP
The option tags should have value attributes.

Remove the dot after jsp?, i.e. make it "...jsp?option=this.value".

In the JSP, check if the option value is set. If it isn't, display the default options. If it is, display the first combo box as normal (with the selected option set) and populate the second combo box using the passed option.
Feb 25 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3384
by: Bob Alston | last post by:
In 2002, "GrayJay" posted the following code: I did this in a jazz record catalogue to find composers - On a form "frmComposers" Create a text box - txtFindComposer, and add the following sub...
2
1536
by: News charter | last post by:
I'm playing with MS Visual Studio C# I have a Combo box that drops down to let me pick a quantity value. called milkQnty However, my understanding is that C# treats this as an object. I want...
1
10188
by: newbie | last post by:
Hello, I am using a propertygrid to allow users to edit\create objects at runtime, that can update records in a database. There are certain properties (fields) that I wish to display as...
2
3333
by: John Ninan | last post by:
I am creating Dynamic Usercontrol in Asp.net application. In this application I have a combobox(aspx Page). Which contains various items. Based on item selected I am dynamically populating...
11
22573
by: simon | last post by:
hello, I have a form that has 10 dropdown lists in one section each of the dropdowns contain the same data what i'm looking to do is that when a user selects a value from one dropdown, change...
2
9656
by: Newbie | last post by:
I have a combobox on a form. Coming from VB, I like to build a dynamic SQL statement and then set the control's data source to the SQL statement. Please tell me how to do that in access using...
0
862
by: Masamune | last post by:
I have an old page that I've created in ASP, but need to recreate in ASP.NET. The ASP page has a combobox at the top that lets you select between 1 and 10 cars. Below is a <%For Idx = 1 to...
3
3204
by: fedya | last post by:
I am trying to have the last 12 months to always be the option in the dropdown for a combo box. (Basically a combobox, with dynamic options) I am using Access 2000. What is the function and...
0
1014
by: Davilean | last post by:
Hi, I have got a combobox which directs to another page on select and when the submit button is clicked. I am trying to change it into a dynamic combobox which redirects you to another page on...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.