change values dynamically

i am using jsp and html for my project.I have one problem. If i select an option from a drop down box the values in the other drop down box should change values accordingly, that too it has to load the values from database. can you help me out with this.
[261 byte] By [minerva_7a] at [2007-11-26 17:53:11]
# 1
http://forum.java.sun.com/thread.jspa?forumID=45&threadID=5132796
beradriana at 2007-7-9 5:06:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
I saw that post. But it is with XML. I want it in SQL. Can you help me out?
minerva_7a at 2007-7-9 5:06:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
not even in drop down . if i an retrieve the value in a text box then also ok.
minerva_7a at 2007-7-9 5:06:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi,

If you are planning to use javascript.Please see the code below.

<html>

<body>

<form name="form1">

Select a City:

<select name="city" onchange="changePlaces()">

<option value="vijaywada">Vijaywada

<option value="tenali">Tenali

</select>

Select a Place:

<select name="places">

</select>

</form>

</body>

</html>

<script>

function changePlaces() {

var selectedCity=document.form1.city.options[document.form1.city.selectedIndex].value;

if(selectedCity=="tenali") {

option1 = new Option("gov","governorpet");

option2 = new Option("besant","besant road");

document.form1.places.options[0] = option1;

document.form1.places.options[1] = option2;

}

else {

option1 = new Option("bose","Bose Road");

option2 = new Option("ratna","Ratna Theatre");

document.form1.places.options[0] = option1;

document.form1.places.options[1] = option2;

}

}

</script>

prasanth_vijayawadaa at 2007-7-9 5:06:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Simply use a javascipt funtion and acll this function in onchange wvent of list box.

example function is given below:-

function selSkillType()

{if(document.form1.cmbSkillType.value=='')

{}

else

{document.form1.action="firstForm.jsp?index1="+document.form1.cmbSkillType.value;

document.form1.submit();

}

}

when you call this function it will submit to the same page.but that time its url has the id of selected list box value.

using request.getParameter method u can access that value.with that value you can execute any sql query.

but the problem is when you load the page first time there is no variable in the url.so y have to check this with a strong if condition.

i think this will give you a small idea.

if you don't get any thing ,i can give you some demo jsp pages .please inform me

thank you

JSP_Todlera at 2007-7-9 5:06:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
with this code i am not able retrieve the value. i feel nothing happens when i change the dropdown box. should i use this code in onchange event of the dropdown box? can you explain me more?
minerva_7a at 2007-7-9 5:06:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
HI Try using the AJAX request for fetchning the values from the database for the second drop down. You can get the ready code from so many sites. Thats the best approach, without submitting the form.
maduri.srinivasa at 2007-7-9 5:06:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
i got the answer.i used combo.value. but i need to use combobox.options[combobox.selectedIndex].textand need to use form.targetits working. thanks for your help.
minerva_7a at 2007-7-9 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
in the example u have given is simple i mean u know the values so u can embed in the code. if i the list stored in the database then can i retrive from database. if yes the how should i get the value in java script. if u can pls help me out
sreenivasaa at 2007-7-9 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

<label>

<select name="party" size="1" onChange="document.forms[0].elements['city'].selectedIndex =this.selectedIndex">

<option >Select</option >

<%while(rs1.next()){%>

<option value="<% out.write(rs1.getString("name1")); %>" >

<% out.write(rs1.getString("name1")); %>

</option>

<option value="<% out.write(rs1.getString("name2")); %>" >

<% out.write(rs1.getString("name2")); %>

</option>

<%}%>

</select>

</label>

<strong> </strong> </td>

<td width="440"><strong>City:

<label>

<select name="city" size="1" style="class2" >

<option>Select</option>

<span class="style3">

<%

String db1 ="SELECT city1,city2,id From listing";

ResultSet rs2 =statement.executeQuery(db1);

%>

<%while(rs2.next()){%>

<option value="<% out.write(rs2.getString("city1")); %>" >

<% out.write(rs2.getString("city1")); %>

</option>

<option value="<% out.write(rs2.getString("city2")); %>" >

<% out.write(rs2.getString("city2")); %>

</option>

<%}%>

</select>

</span>

</label>

Pragnesha at 2007-7-9 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

here is the code for changing the content of combo box dynamically taking data from data base with out submit.

this uses string buffer in jsp page and pass it to a function in java script.

<%

ArrayList statuslist = (ArrayList)request.getSession().getAttribute("alleqpt");

StringBuffer sb = new StringBuffer();

String[] str1 = new String[statuslist.size()] ;

String[] str2 = new String[statuslist.size()] ;

/// this is to put it in a string buffer

Iterator it=statuslist.iterator();

System.out.println(statuslist.size());

for(int i=0;i<statuslist.size();i++)

{

String[] str = (String[])it.next();

str1 = str[0];

str2= str[1];

sb.append("'"+str2+"'"+",'"+str1+"',");

}

sb.deleteCharAt(sb.length()-1);

System.out.println(sb);

%>

<script language="javascript" type="">

function getEqpttype()

{

var x = document.getElementById('eqptlst');

var y = x.value;

var tryonce = document.getElementById('eqpttypelst');

tryonce.length = 0;

var array = new Array(<%=sb%>)

for(var i=0;i<array.length;i++)

{

if(y==array[i+1])

{

tryonce.options[tryonce.length] = new Option(array)

}

}

}

></script>

sreenivasaa at 2007-7-9 5:06:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...