aspose file tools*
The moose likes JSP and the fly likes populating one dropdown box depending on the value of other dropdown box  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "populating one dropdown box depending on the value of other dropdown box  " Watch "populating one dropdown box depending on the value of other dropdown box  " New topic
Author

populating one dropdown box depending on the value of other dropdown box

payal Jain
Ranch Hand

Joined: Dec 20, 2007
Posts: 50
Hi All,
I have two dropdown boxes say A and B in jsp.In both ,values has to be retrived from database.
I filled box A with values,Now when I select a value in box A ,values in box B should get populated accordingly.

I have no idea how to achieve it.
Please help me out....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

Do you want to refresh the page in order to accomplish this? If so, then it can be done with JSP. Otherwise, you'll need to use JavaScript and Ajax. Which is your preference?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
payal Jain
Ranch Hand

Joined: Dec 20, 2007
Posts: 50
Yes,I want the page to be refreshed.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

Then, in your JSP, you can test for the inclusion of a scoped variable that contains the data (a list or map, most likely) for the second dropdown. If it's there you render the dropdown. Otherwise not.

During the first run of the JSP, the variable will not be present and the select can be left empty and disabled. After a value is chose from the first dropdown, the relevant data is obtained and added to the request as the scoped variable and the page is re-rendered. This time, because the data is present, the dropdown will be rendered.
payal Jain
Ranch Hand

Joined: Dec 20, 2007
Posts: 50
thanks for the response,
but can you please explain me with code how to do it.

Payal.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

JavaRanch doesn't work that way. You write the code and we help you with it.
payal Jain
Ranch Hand

Joined: Dec 20, 2007
Posts: 50
here's the code I tried
<html>
<body>
<form action="addToCart.jsp">
<jsp:useBean id="car" class="beanClasses.AddToCart" scope="request">
<jsp:setProperty name="cars" property="*"/>

</jsp:useBean>
<select name="cars" onchange="">
<% Array list =car.getList();//List of cars from bean class
int lengthArray=array.length;
for(int x=0;x<lengthArray;x++)
{
System.out.println("Value is"+array[x].toString());
%>

<option value="<%=array[x].toString()%>"><%=array[x].toString()%></option>
<% }
%>

</select>
</select>//2nd dropdown which remains diasble initially untill value is selected
<select name="year" disabled="disabled" >

<option value="<%=car.getYear()" selected="selected"><%=car.getYear %></option>

</select>
</body>
</html>
Now how to get the selected value from one dropdown in the bean class so accordingly I can get the value for other dropdown.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

Two things:

1) What version of JSP are you using? If it's JSP 2.0, you should no longer be using scriptlets in your pages, but rather the JSTL and EL.

2) Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along. Please read this for more information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: populating one dropdown box depending on the value of other dropdown box