Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to create a form with two drop-down lists (country and town), filled with options from two mysql tables. And I would like the second list (towns) to be populated with options depending on the value of selected option on the first list (country).

I know how to load the options with php and mysql into the countries list and I can link this to a second list that is populated with options from a javascript. There are conditionals in the script that updates the values of the second list. But I need some help with the mysql query for the second list. How can I start a second query when the page is already loaded?

share|improve this question

3 Answers

up vote 0 down vote accepted

You've got two general ways of going about this.

First, is to include all of the values into the page possibly as a javascript array, then as the first list is changed, it just re-adds the appropriate values to the second drop-down. If you use this method, every item (from both tables/lists) is loaded on every request. If the total number of items is relatively small (say <100) then probably your best approach.

The other option is to issue an ajax request after the first dropdown is selected. The request would contain the id/value of the dropdown, and would return the items that are valid for that value. This method means you only need one query on the initial page load (the values for the first dropdown) but also requires you to create a javascript handler.

I've seen both approaches used. The first one is usually better for smaller datasets, but can be faster since the query is static (no parameters), and can be cached. However, you then transmit that data on every request. The second one is slightly "cleaner" but requires at least one ajax request. If you are already familiar with using that approach then probably your best option.

share|improve this answer

http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html

http://roshanbh.com.np/dropdown/

Please look around to see if your question has already been asked (and maybe even answered!) before you ask.

Use search funtioanlity on right top or at the time of writing the question title you can see somerelated questions.

share|improve this answer
@jball stackoverflow.com/faq @SO Please look around to see if your question has already been asked (and maybe even answered!) before you ask. Its not wrong to look around in google too. This question is not at all a programming doubt or he stuck somewhere.he is simply asking something is repetedly asked here many times – zod Nov 15 '10 at 15:12
@zod please read meta, and treat the newbies nicely. (see embrace the non-googlers and How to deal with Google questions specifically). – jball Nov 15 '10 at 15:26
@jball .. hmmm i edit my answer. you happy now !!!. anyway thanks for the info. – zod Nov 15 '10 at 15:37
@zod, just trying to help make SO a good place for all coders :) – jball Nov 15 '10 at 15:43
@jball .yeah. SO is really good place with better and fast responses from better coders. Then one thing i notice about you. even your profile pic is a pic of downvote (-ve) :) Are you a downvoter like the undertaker in wwe. :) Take it easy man. :) – zod Nov 15 '10 at 15:55
show 1 more comment

you can have a look at this as well

http://plugins.jquery.com/project/doubleselect

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.