• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,I want the page to be refreshed.
 
Bear Bibeault
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the response,
but can you please explain me with code how to do it.

Payal.
 
Bear Bibeault
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic