Dropdowns
Date: 12/14/05
(Javascript Community) Keywords: php, mysql, database, sql, java
Hi, I have some questions regarding the best way to go about creating dynamic dropdowns in javascript.
I have to create a form with two dropdown lists. One for US states the other for counties within those states. The county dropdown will remain blank until the user selects a state, then the county dropdown will be populated with the correct counties for that state.
I have this working using PHP (the rest of the application is PHP) but I want to do it without reloading the page. The list of states and counties are stored in 2 tables in a mysql database.
My first instinct is use PHP to write the javascript for me. Do the DB work with PHP and then write the javascript arrays. But that seems wrong.
I would think that the javascript should go something like this and would be called from an onchange handler in the state dropdown. I dont know the exact syntax.
create function setOptions(id)
create array of counties indexed with their id's from the database
if there is an id passed to the function
iterate through the array of counties
if a key matches the id then write that to the options tag of the county select list
Is that the right direction? Any pointers would be much appreciated.
Thanks
Source: http://community.livejournal.com/javascript/88041.html