[solve]dynamic values for select option

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tkarven
Forum Commoner
Posts: 41
Joined: Tue Aug 02, 2005 10:26 pm

[solve]dynamic values for select option

Post by tkarven »

hi,

i got two select in my page, and i would like the second select to have it's value dynamically changed based on values for first select.

e.g. my first select is country while my second select one is state. so obviously whenever the user specify a country the second select will changed automatically.

can anyone specify a direction or keyword for me to overcome this scenario ?

thx a lot..
Last edited by tkarven on Mon Aug 08, 2005 11:13 pm, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

SELECT * FROM countries 
INNER JOIN states 
USING (country_id)
WHERE countries.name = 'Canada'
This is assuming that you have a key connecting the states and the country (country_id) and your countries tables name column is `name`. To dynamically change 'Canada' change the value to $_GET['name'] and pass the value through the query string..

so countries.php?name=Canada

Code: Select all

$sql = 'SELECT * FROM countries 
INNER JOIN states 
USING (country_id)
WHERE countries.name = \''.$_GET['name'].\'';
tkarven
Forum Commoner
Posts: 41
Joined: Tue Aug 02, 2005 10:26 pm

Post by tkarven »

hmmh, i got no prob with the database linking. my question was, how to us update the second select, whenever the user specify a country in the first select ? anyway thx for ur reply
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Maybe CoderBoglin will help you in his tutorial
viewtopic.php?t=29084

READ BEFORE POST
Last edited by harrisonad on Mon Aug 08, 2005 10:17 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$sql = 'SELECT * FROM countries
INNER JOIN states
USING (country_id)
WHERE countries.name = \''.$_GET['name'].''; 

if (isset($_GET['state'])) {
   $sql .= ' AND states.name = \''.$_GET['state'].'\'';
}
Im probably being thick, but can you rephrase your question. I don't really understand what your asking for.
tkarven
Forum Commoner
Posts: 41
Joined: Tue Aug 02, 2005 10:26 pm

Post by tkarven »

okie viewtopic.php?t=29084 exactly wad i looking for.

thx all
Post Reply