Forum Moderators: coopster

Message Too Old, No Replies

Dynamically populate drop downlist from another dropdown list

Froms input control management

         

pcwow

7:57 pm on May 11, 2009 (gmt 0)

10+ Year Member



my form has the following structure

for country...
<div>
<label for="country">Country</label>
<select name="country">
<option value="unknown" selected>select a country</option>
<option value="US">United States</option>
<option value="AF">Afghanistan</option>
...
(all other countries here)
...
<option value="ZM">Zambia</option>
<option value="ZW">Zimbabwe</option>
</select>
</div>

for State
<div>
<label for="states">States</label>
<select name="state">
<option value="unknown" selected>select a state</option>
?
</select>
</div>

What I need
NOTE: The script will be stored into a script.js file outside the html page for the form...

1) In case of selecting US or BR only this two, I need to make the states for all US states or BR states accordingly
2) if other then US or BR is selected, state field is to be desabled (Only interested on the state for this two countries) and state value should be "unknown"
3) if US or BR selected It must select a state to validade te the form.... (locally using javascript, server side using php)

I hope somewhone with time to dig into it can give me an Arm! I know I'm asking more than just a hand... ;)

rocknbil

2:38 am on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, let's get a couple things out of the way . . . they may seem minor but play into the big picture of what you're trying to do . . .


<label for="country">Country</label>
<select name="country">

Labels are for ID's. You need

<label for="country">Country</label>
<select name="country" id="country">

The ID attribute gets important in the Javascript, below.

2) if other then US or BR is selected, state field is to be desabled (Only interested on the state for this two countries) and state value should be "unknown"

Don't disable it; on select of the country, if it's not US, just don't output the state field from your PHP. This leads to an answer here:


3) if US or BR selected It must select a state to validade te the form.... (locally using javascript, server side using php)

So if there's no state output in the form, in your Javascript you can do

if (document.getElementById('state')) {
// validate selectedIndex > 0
}

(Remember what I said about the ID attribute?)

So if "state" is not in the form, it would never execute the above JS.

I think the answers to the overall questions might be found in this thread [webmasterworld.com] in which we discuss the logic of outputting lists that depend on other parent lists.