Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Dynamic Drop Down Lists.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Gurus,

I am trying to build a JSP page with two drop down lists. The values in the second drop down list should be loaded from the database based on the value selected in the first drop down list. I have two tables 1st one is Customers and in that I have fields : ItemId and CustomerId, my second table is Items and in that I have fields : ItemId and Itemname. So, in my first drop down list I need the values from ItemId in the Customers table to be loaded when the user first accesses the page and when an ItemId is selected in the first drop down list it should pull the Itemname(s) from the Items table and populate the second drop down list. Hope that makes sense. Any help in this matter would be much appreciated.

Cheers,

Mike.
 
Sheriff
Posts: 67619
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
This has been discussed many times in this and the HTML forum. Poke around a bit and you will find lots of good ideas.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
I've just added a demo project that shows how to do this using an MVC pattern.
It's packaged as a war file so you can just drop it into a running instance of Tomcat and test it.

http://simple.souther.us/not-so-simple.html
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,

You use two different forms to do this. I have a case where the drop downs in question are part of one big form. How do I handle such a case ?

Thanks for your help !

-Sri
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both select lists are in the same form.
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben ! I guess it works the same, then.

Now, you do some logic in the back-end to keep the drop down item the user selected to be selected when the page comes back.

So, if I have other form input text fields, checkboxes and what not, that the user has entered, before I send the request to the server to populate the second drop-down, I guess I have to send them back as well, so that the form does not get "reset". Am I right ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every time you submit the form, the selected value from each select list gets send with the rest of the form information.

In that demo, I build the select list option tags in an option and then read all of them as one bean property but you could implement the same logic with JSTL and EL or (dare I say it...) scriptlets in your JSP.


[ May 18, 2005: Message edited by: Ben Souther ]
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

I am thinking of collecting the selected values(including text fields, checkboxes, radio buttons, etc) in a bean( I am using Struts) and placing the bean in request scope after the first form submit. I can use these values in the JSP to pre-popluate the form when it gets displayed the second time.

Is there an easier way ? :-) I will look into JSTL and EL.
 
reply
    Bookmark Topic Watch Topic
  • New Topic