Jump to content

help with function to select state/region associated with picked country


Patrick817

Recommended Posts

Hello Everyone.

 

I am trying to adjust code in a function to take a selected country (on a registration page) and then display the associated states/regions in that country.  I have already compiled the data but cannot figure out how to call it appropriately. 

 

What I am getting is the select of all countries for a user to pick.  But, the state field still shows all states/regions in the State table. 

 

What I would like is for the user to select a country and then the state/region field to only show the states/regions associated with the country (from the Country.Country Name and States.State Abbreviation)

 

There are two tables:  Country (fields:  Country Abbreviation, Country Name)  (Example:  US, United States)

                                    States (fields:  State ID (autonumber), State Name, State Abbreviation, Country Code, Country Name)

 

Here is the original function code:

 

  function country_select($selected) {

    ?><option value="0"<?php if ($selected == "") { ?> selected="selected"<?php } ?>>-Select One-</option><?php

    $states = query_mysql("SELECT `Country Abbreviation`, `Country Name` FROM `Countries`");

    while ($state = mysql_fetch_array($states)) {

      ?><option value="<?php ehtml($state["Country Abbreviation"]); ?>"<?php if ($selected == $state["Country Abbreviation"]) { ?> selected="selected"<?php } ?>><?php ehtml($state["Country Name"]); ?></option><?php

    }

  }

 

 

  function state_select($selected) {

    ?><option value="0"<?php if ($selected == "") { ?> selected="selected"<?php } ?>>-Select One-</option><?php

    $states = query_mysql("SELECT `State Abbreviation`, `State Name` FROM `States`");

    while ($state = mysql_fetch_array($states)) {

    ?><option value="<?php ehtml($state["State Abbreviation"]); ?>"<?php if ($selected == $state["State Abbreviation"]) { ?> selected="selected"<?php } ?>><?php ehtml($state["State Name"]); ?></option><?php

    }

  }

 

Any ideas?

 

Thanks,

 

Patrick

Well, your second query is always going to select all the states. Because of the way the query is written.

 

The only way to do it, would be a page refresh after a country is picked, that would then load the second select box. Unless of course you use Ajax, take a look in to chained select boxes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.