wood burning stoves 2.0*
The moose likes JSP and the fly likes toughie..JSP in Javascript Dynamic Drop Down List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "toughie..JSP in Javascript Dynamic Drop Down List" Watch "toughie..JSP in Javascript Dynamic Drop Down List" New topic
Author

toughie..JSP in Javascript Dynamic Drop Down List

Howard Tan
Greenhorn

Joined: Dec 11, 2003
Posts: 4
hello, i have 2 drop down lists where the 2nd drop down list is dependent on the first drop down list. The 2nd drop down list retreives data from a database while the 1st consists of default values. The data of both drop down list are recorded on the same table in separate fields in the database.
Drop down list 1 has category A,B,C,D. Depending on the selection, a set of student offenses are displayed in the 2nd drop down list.
I have tried programming jsp in javascript but I really don't know the proper syntax.
What I know:
1. will need dynamic arrays like vectors because the data in the 2nd drop down list can be modified(added,deleted)
2. need to SELECT Offense_code and Offense_Description
3. then create a 2d array, where the 1st dimension totals the number of categories(4) and the 2nd dimension totals the number of offense codes(dynamic).
4. then equating each 2d array element with the corresponding offense description.
Question:
1. When to use "%>" and "<%" in javascript
2. Where can i place SELECT statements in javacript?
3. Should I use javascript loops, condition statements instead of jsp?
4. Do I make the array in javascript or jsp?
4. How to differentiate javacript variables and jsp variables?
this is my code..i know its wrong but I just dont know how to fix it.
<%

coSQLStatement = "SELECT category, offenseCode, description FROM Offense";
rsCategoryOffense = stmtCO.executeQuery(coSQLStatement);
%>
<script>
<!--
var aCategoryOffense = new Array();
var aCatCode = new Array();
<%
while (rsCategoryOffense.next())
{if (rsCategoryOffense.getString(4).equals("A"))
{
aCategoryOffense[] = new Array();
aCategoryOffense[0][j] = rsCategoryOffense.getString(3);
aCatCode[0][j] = rsCategoryOffense.getString(1);
}
else if (rsCategoryOffense.getString(4).equals("B"))
{
aCategoryOffense[] = new Array();
aCategoryOffense[1][j] = rsCategoryOffense.getString(3);
aCatCode[1][j] = rsCategoryOffense.getString(1);
}
else if (rsCategoryOffense.getString(4).equals("C"))
{
aCategoryOffense[] = new Array();
aCategoryOffense[2][j] = rsCategoryOffense.getString(3);
aCatCode[2][j] = rsCategoryOffense.getString(1);
}
else if (rsCategoryOffense.getString(4).equals("D"))
{
aCategoryOffense[] = new Array();
aCategoryOffense[3][j] = rsCategoryOffense.getString(3);
aCatCode[3][j] = rsCategoryOffense.getString(1);
}
j++;
}
%>
function catOffense{
offense-code.options = aCategoryOffense[offense-category.selectedIndex];
}
//-->
</script>
Howard Tan
Greenhorn

Joined: Dec 11, 2003
Posts: 4
Btw, I know I have to use the onChange function for the first drop down list.
just a cut of the code here...
<td width="52"><select name="offense-category" id="offense-category" onChange="catOffense();">
<option value="A" selected>A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
<td width="64" class="text"> <select name="offense-code" class="forms">
the 2nd drop down list data should display in form "offense-code".
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12680
    
    5
I have tried programming jsp in javascript but I really don't know the proper syntax.

That would indicate some confusion on your part. In Javascript you can only do things on the user browser side. In JSP you can only do things on the web server side - "never the twain shall meet."
I suggest you create a complete HTML prototype of the web page you want to build, with Javascript and mock-up data. When that is working, convert it step by step (NOT all at once) to JSP. Add your database stuff last - after the user interface is working.
Bill


Java Resources at www.wbrogden.com
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can go two ways with this.
First way 100% Javascript. You have the jsp right all of the arrays for the double combo and the javascript does the rest of the work. You can get a JavaScript double combo at http://www.dynamicdrive.com
Second way is to resubmit the page to itself and pull the information for the seecond dropdown onto the page.
Method one is alot faster since the page does not need to be reloaded and all the information is there from the start.
Method two has to worry about loosing form information and other facts about surfing the page.
Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toughie..JSP in Javascript Dynamic Drop Down List
 
Similar Threads
help with game
dynamic drop down list values
problem while retrieving 2d array
How to disply in the page
How to pass dynamic array content from javascript to jsp