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.

Who is Participating?
Here is a how-to i found that goes through and explains all of the necessary code:


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.
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.
joedunnAuthor Commented:
How can I do this using Ajax?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.