• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toughie..JSP in Javascript Dynamic Drop Down List

 
Howard Tan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic