[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 693
  • Last Modified:

Drop Down Box Selection Populates Another Drop Down Box

The effect I want to create is really simple, however, doing it is not, at least for me.

I have a form on a page.  When a selection in combo box 1 is made it populates combo box number 2 then 3 then 4.

Like this...

Box 1 - Country
Box 2 - State
Box 3 - City
Box 4 - Zip Code


So for this example let assume I make the following choices:

Box 1:  United States (All countries would be displayed in Box 1)
Box 2:  Idaho (All 50 US States would have been available
Box 3:  Boise (A list of cities that I wanted for Idaho would be here)
Box 4:  83705 (a List of available zip codes for Boise, ID, USA)

I know I have to supply the lists for each box, that is easy, I have the data.  

#1.  How can I do this?
#2.  Is it possible to do this via a connection to a DB where each table in the DB contains information for a different field?  That would be the best but I'll take whatever I can get help with.

Thanks
0
joedunn
Asked:
joedunn
  • 2
1 Solution
 
bradl3yCommented:
This could all be done without much javascript if you don't mind reloading the page after each selection. Set the onchange attribute of the select element to submit the form. Submitting the form.

The page would load, write php to generate a second selection box based on what was submitted for the first.

If you want to do this without reloading, you would need to use an AJAX solution. Onchange of the select element, send the value to the server, have the server generate the code (using PHP if you wish) for the second drop down box that will be returned to the page via AJAX, and use javascript to replace the second drop down list with the new list.
0
 
joedunnAuthor Commented:
How can I do this using Ajax?
0
 
bradl3yCommented:
Here is a how-to i found that goes through and explains all of the necessary code:

http://www.webmonkey.com/tutorial/Build_an_Ajax_Dropdown_Menu

And here is a working example of said code: http://duvinci.com/projects/ajax/dropdowns/

Note that his script is fairly bare-bones, and pulls data from a flat text file, rather than a database. It should be fairly trivial to use PHP to connect to your database rather than pull data from a text file.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now