File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
payal Jain
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
payal Jain
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,I want the page to be refreshed.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the response,
but can you please explain me with code how to do it.

Payal.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaRanch doesn't work that way. You write the code and we help you with it.
 
payal Jain
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic