This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 50
  • 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....
 
Marshal
Posts: 67224
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,I want the page to be refreshed.
 
Bear Bibeault
Marshal
Posts: 67224
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • 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
Marshal
Posts: 67224
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • 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
Marshal
Posts: 67224
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!