473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

country,state,city selection

hi friends...

in my php page 3 dropdownlist boxes are there.. ie country,state,city..

if country is selected, in onchange function.. corresponding states has to be populated in state dropdownlist box.. likewise if particular state is selected corresponding city has to be populated in 3rd list box..

can u help me??

thank you...
Mar 28 '09 #1
11 17937
Amzul
130 100+
sounds like you need to use Javascript AJAX
onclick or onchange event on the first select box (countrys) will trigger the populate of the second dropbox (stats...) ...
read here to get you started
Mar 28 '09 #2
Markus
6,050 Expert 4TB
AJAX is pointless for a task like this.

If you know ever possible country, state, city, etc., it'd be more efficient to store it in a javascript array, I'd expect.
Mar 28 '09 #3
numberwhun
3,509 Expert Mod 2GB
Ok, so here is another solution. How about populating each country and its 2 letter code into an PHP associative array and then using it in a loop to produce the code needed for the select statement. Just an idea, seeing as how this is a PHP forum. :)

Or, you can just grab the finished product here.

You can do the same for states, in fact here is an example of what I was referring to above, but with a non-associative array.

With a few minor changes, you can produce a country select box that produces the same thing. As for the city select, that is quite a bit more entailed and would probably consist of a lot of tedious coding considering the number of cities in each country. The city might be better served as a user entered field.

Regards,

Jeff
Mar 28 '09 #4
numberwhun
3,509 Expert Mod 2GB
Call me a bit anal but I wanted to share with you a couple of functions I wrote today. I am slowly building my repertoire of php functions and this thread prompted some additions for forms.

Here is the function I now have for producing a state select box:

Expand|Select|Wrap|Line Numbers
  1.     function state_selection() {
  2.     echo '<select name="state">'."\n";
  3.  
  4.         $states = array(
  5.             'Alabama' => 'AL',
  6.             'Alaska' => 'AK',
  7.             'American Samoa' => 'AS',
  8.             'Arizona' => 'AZ',
  9.             'Arkansas' => 'AR',
  10.             'California' => 'CA',
  11.             'Colorado' => 'CO',
  12.             'Connecticut' => 'CT',
  13.             'Delaware' => 'DE',
  14.             'District of Columbia' => 'DC',
  15.             'Federated States Of Micronesia' => 'FM',
  16.             'Florida' => 'FL',
  17.             'Georgia' => 'GA',
  18.             'Hawaii' => 'HI',
  19.             'Idaho' => 'ID',
  20.             'Illinois' => 'IL',
  21.             'Indiana' => 'IN',
  22.             'Iowa' => 'IA',
  23.             'Kansas' => 'KS',
  24.             'Kentucky' => 'KY',
  25.             'Louisiana' => 'LA',
  26.             'Maine' => 'ME',
  27.             'Marshall Islands' => 'MH',
  28.             'Maryland' => 'MD',
  29.             'Massachusetts' => 'MA',
  30.             'Michigan' => 'MI',
  31.             'Minnesota' => 'MN',
  32.             'Mississippi' => 'MS',
  33.             'Missouri' => 'MO',
  34.             'Montana' => 'MT',
  35.             'Nebraska' => 'NE',
  36.             'Nevada' => 'NV',
  37.             'New Hampshire' => 'NH',
  38.             'New Jersey' => 'NJ',
  39.             'New Mexico' => 'NM',
  40.             'New York' => 'NY',
  41.             'North Carolina' => 'NC',
  42.             'North Dakota' => 'ND',
  43.             'Northern Mariana Islands' => 'MP',
  44.             'Ohio' => 'OH',
  45.             'Oklahoma' => 'OK',
  46.             'Oregon' => 'OR',
  47.             'Palau' => 'PW',
  48.             'Pennsylvania' => 'PA',
  49.             'Puerto Rico' => 'PR',
  50.             'Rhode Island' => 'RI',
  51.             'South Carolina' => 'SC',
  52.             'South Dakota' => 'SD',
  53.             'Tennessee' => 'TN',
  54.             'Texas' => 'TX',
  55.             'Utah' => 'UT',
  56.             'Vermont' => 'VT',
  57.             'Virgin Islands' => 'VI',
  58.             'Virginia' => 'VA',
  59.             'Washington' => 'WA',
  60.             'West Virginia' => 'WV',
  61.             'Wisconsin' => 'WI',
  62.             'Wyoming' => 'WY'
  63.     );
  64.  
  65.         foreach ($states as $state_name => $state_abbr) {
  66.             echo '<option value="'.$state_abbr.'">'.$state_name.'</option>'."\n";
  67.     }
  68.  
  69.         echo '</select>'."\n";
  70.     }
  71.  
And here is the function I have for producing a country select list. I took this from the ISO so it should be pretty current:

Expand|Select|Wrap|Line Numbers
  1.     function country_selection() {
  2.         echo '<select name="country">'."\n";
  3.  
  4.         $countries = array(
  5.             "AFGHANISTAN" => "AF",
  6.             "ÅLAND_ISLANDS" => "AX",
  7.             "ALBANIA" => "AL",
  8.             "ALGERIA" => "DZ",
  9.             "AMERICAN_SAMOA" => "AS",
  10.             "ANDORRA" => "AD",
  11.             "ANGOLA" => "AO",
  12.             "ANGUILLA" => "AI",
  13.             "ANTARCTICA" => "AQ",
  14.             "ANTIGUA_AND_BARBUDA" => "AG",
  15.             "ARGENTINA" => "AR",
  16.             "ARMENIA" => "AM",
  17.             "ARUBA" => "AW",
  18.             "AUSTRALIA" => "AU",
  19.             "AUSTRIA" => "AT",
  20.             "AZERBAIJAN" => "AZ",
  21.             "BAHAMAS" => "BS",
  22.             "BAHRAIN" => "BH",
  23.             "BANGLADESH" => "BD",
  24.             "BARBADOS" => "BB",
  25.             "BELARUS" => "BY",
  26.             "BELGIUM" => "BE",
  27.             "BELIZE" => "BZ",
  28.             "BENIN"  => "BJ",
  29.             "BERMUDA" => "BM",
  30.             "BHUTAN" => "BT",
  31.             "BOLIVIA" => "BO",
  32.             "BOSNIA AND HERZEGOVINA" =>  "BA",
  33.             "BOTSWANA" => "BW",
  34.             "BOUVET ISLAND" => "BV",
  35.             "BRAZIL" => "BR",
  36.             "BRITISH INDIAN OCEAN TERRITORY" => "IO",
  37.             "BRUNEI DARUSSALAM" => "BN",
  38.             "BULGARIA" => "BG",
  39.             "BURKINA FASO" => "BF",
  40.             "BURUNDI" => "BI",
  41.             "CAMBODIA" => "KH",
  42.             "CAMEROON" => "CM",
  43.             "CANADA" => "CA",
  44.             "CAPE VERDE" => "CV",
  45.             "CAYMAN ISLANDS" => "KY",
  46.             "CENTRAL AFRICAN REPUBLIC" => "CF",
  47.             "CHAD" => "TD",
  48.             "CHILE" => "CL",
  49.             "CHINA" => "CN",
  50.             "CHRISTMAS ISLAND" => "CX",
  51.             "COCOS (KEELING) ISLANDS" => "CC",
  52.             "COLOMBIA" => "CO",
  53.             "COMOROS" => "KM",
  54.             "CONGO" => "CG",
  55.             "CONGO, THE DEMOCRATIC REPUBLIC OF THE" => "CD",
  56.             "COOK ISLANDS" => "CK",
  57.             "COSTA RICA" => "CR",
  58.             "CÔTE D'IVOIRE" => "CI",
  59.             "CROATIA" => "HR",
  60.             "CUBA" => "CU",
  61.             "CYPRUS" => "CY",
  62.             "CZECH REPUBLIC" => "CZ",
  63.             "DENMARK" => "DK",
  64.             "DJIBOUTI" => "DJ",
  65.             "DOMINICA" => "DM",
  66.             "DOMINICAN REPUBLIC" => "DO",
  67.             "ECUADOR" => "EC",
  68.             "EGYPT" => "EG",
  69.             "EL SALVADOR" => "SV",
  70.             "EQUATORIAL GUINEA" => "GQ",
  71.             "ERITREA" => "ER",
  72.             "ESTONIA" => "EE",
  73.             "ETHIOPIA" => "ET",
  74.             "FALKLAND ISLANDS (MALVINAS)" => "FK",
  75.             "FAROE ISLANDS" => "FO",
  76.             "FIJI" => "FJ",
  77.             "FINLAND" => "FI",
  78.             "FRANCE" => "FR",
  79.             "FRENCH GUIANA" => "GF",
  80.             "FRENCH POLYNESIA" => "PF",
  81.             "FRENCH SOUTHERN TERRITORIES" => "TF",
  82.             "GABON" => "GA",
  83.             "GAMBIA" => "GM",
  84.             "GEORGIA" => "GE",
  85.             "GERMANY" => "DE",
  86.             "GHANA" => "GH",
  87.             "GIBRALTAR" => "GI",
  88.             "GREECE" => "GR",
  89.             "GREENLAND" => "GL",
  90.             "GRENADA" => "GD",
  91.             "GUADELOUPE" => "GP",
  92.             "GUAM" => "GU",
  93.             "GUATEMALA" => "GT",
  94.             "GUERNSEY" => "GG",
  95.             "GUINEA" => "GN",
  96.             "GUINEA-BISSAU" => "GW",
  97.             "GUYANA" => "GY",
  98.             "HAITI" => "HT",
  99.             "HEARD ISLAND AND MCDONALD ISLANDS" => "HM",
  100.             "HOLY SEE (VATICAN CITY STATE)" => "VA",
  101.             "HONDURAS" => "HN",
  102.             "HONG KONG" => "HK",
  103.             "HUNGARY" => "HU",
  104.             "ICELAND" => "IS",
  105.             "INDIA" => "IN",
  106.             "INDONESIA" => "ID",
  107.             "IRAN, ISLAMIC REPUBLIC OF" => "IR",
  108.             "IRAQ" => "IQ",
  109.             "IRELAND" => "IE",
  110.             "ISLE OF MAN" => "IM",
  111.             "ISRAEL" => "IL",
  112.             "ITALY" => "IT",
  113.             "JAMAICA" => "JM",
  114.             "JAPAN" => "JP",
  115.             "JERSEY" => "JE",
  116.             "JORDAN" => "JO",
  117.             "KAZAKHSTAN" => "KZ",
  118.             "KENYA" => "KE",
  119.             "KIRIBATI" => "KI",
  120.             "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF" => "KP",
  121.             "KOREA, REPUBLIC OF" => "KR",
  122.             "KUWAIT" => "KW",
  123.             "KYRGYZSTAN" => "KG",
  124.             "LAO PEOPLE'S DEMOCRATIC REPUBLIC" => "LA",
  125.             "LATVIA" => "LV",
  126.             "LEBANON" => "LB",
  127.             "LESOTHO" => "LS",
  128.             "LIBERIA" => "LR",
  129.             "LIBYAN ARAB JAMAHIRIYA" => "LY",
  130.             "LIECHTENSTEIN" => "LI",
  131.             "LITHUANIA" => "LT",
  132.             "LUXEMBOURG" => "LU",
  133.             "LAO PEOPLE'S DEMOCRATIC REPUBLIC" => "LA",
  134.             "LATVIA" => "LV",
  135.             "LEBANON" => "LB",
  136.             "LESOTHO" => "LS",
  137.             "LIBERIA" => "LR",
  138.             "LIBYAN ARAB JAMAHIRIYA" => "LY",
  139.             "LIECHTENSTEIN" => "LI",
  140.             "LITHUANIA" => "LT",
  141.             "LUXEMBOURG" => "LU",
  142.             "MACAO" => "MO",
  143.             "MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF" => "MK",
  144.             "MADAGASCAR" => "MG",
  145.             "MALAWI" => "MW",
  146.             "MALAYSIA" => "MY",
  147.             "MALDIVES" => "MV",
  148.             "MALI" => "ML",
  149.             "MALTA" => "MT",
  150.             "MARSHALL ISLANDS" => "MH",
  151.             "MARTINIQUE" => "MQ",
  152.             "MAURITANIA" => "MR",
  153.             "MAURITIUS" => "MU",
  154.             "MAYOTTE" => "YT",
  155.             "MEXICO" => "MX",
  156.             "MICRONESIA, FEDERATED STATES OF" => "FM",
  157.             "MOLDOVA, REPUBLIC OF" => "MD",
  158.             "MONACO" => "MC",
  159.             "MONGOLIA" => "MN",
  160.             "MONTENEGRO" => "ME",
  161.             "MONTSERRAT" => "MS",
  162.             "MOROCCO" => "MA",
  163.             "MOZAMBIQUE" => "MZ",
  164.             "MYANMAR" => "MM",
  165.             "NAMIBIA" => "NA",
  166.             "NAURU" => "NR",
  167.             "NEPAL" => "NP",
  168.             "NETHERLANDS" => "NL",
  169.             "NETHERLANDS ANTILLES" => "AN",
  170.             "NEW CALEDONIA" => "NC",
  171.             "NEW ZEALAND" => "NZ",
  172.             "NICARAGUA" => "NI",
  173.             "NIGER" => "NE",
  174.             "NIGERIA" => "NG",
  175.             "NIUE" => "NU",
  176.             "NORFOLK ISLAND" => "NF",
  177.             "NORTHERN MARIANA ISLANDS" => "MP",
  178.             "NORWAY" => "NO",
  179.             "OMAN" => "OM",
  180.             "PAKISTAN" => "PK",
  181.             "PALAU" => "PW",
  182.             "PALESTINIAN TERRITORY, OCCUPIED" => "PS",
  183.             "PANAMA" => "PA",
  184.             "PAPUA NEW GUINEA" => "PG",
  185.             "PARAGUAY" => "PY",
  186.             "PERU" => "PE",
  187.             "PHILIPPINES" => "PH",
  188.             "PITCAIRN" => "PN",
  189.             "POLAND" => "PL",
  190.             "PORTUGAL" => "PT",
  191.             "PUERTO RICO" => "PR",
  192.             "QATAR" => "QA",
  193.             "RÉUNION" => "RE",
  194.             "ROMANIA" => "RO",
  195.             "RUSSIAN FEDERATION" => "RU",
  196.             "RWANDA" => "RW",
  197.             "SAINT BARTHÉLEMY" => "BL",
  198.             "SAINT HELENA" => "SH",
  199.             "SAINT KITTS AND NEVIS" => "KN",
  200.             "SAINT LUCIA" => "LC",
  201.             "SAINT MARTIN" => "MF",
  202.             "SAINT PIERRE AND MIQUELON" => "PM",
  203.             "SAINT VINCENT AND THE GRENADINES" => "VC",
  204.             "SAMOA" => "WS",
  205.             "SAN MARINO" => "SM",
  206.             "SAO TOME AND PRINCIPE" => "ST",
  207.             "SAUDI ARABIA" => "SA",
  208.             "SENEGAL" => "SN",
  209.             "SERBIA" => "RS",
  210.             "SEYCHELLES" => "SC",
  211.             "SIERRA LEONE" => "SL",
  212.             "SINGAPORE" => "SG",
  213.             "SLOVAKIA" => "SK",
  214.             "SLOVENIA" => "SI",
  215.             "SOLOMON ISLANDS" => "SB",
  216.             "SOMALIA" => "SO",
  217.             "SOUTH AFRICA" => "ZA",
  218.             "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS" => "GS",
  219.             "SPAIN" => "ES",
  220.             "SRI LANKA" => "LK",
  221.             "SUDAN" => "SD",
  222.             "SURINAME" => "SR",
  223.             "SVALBARD AND JAN MAYEN" => "SJ",
  224.             "SWAZILAND" => "SZ",
  225.             "SWEDEN" => "SE",
  226.             "SWITZERLAND" => "CH",
  227.             "SYRIAN ARAB REPUBLIC" => "SY",
  228.             "TAIWAN, PROVINCE OF CHINA" => "TW",
  229.             "TAJIKISTAN" => "TJ",
  230.             "TANZANIA, UNITED REPUBLIC OF" => "TZ",
  231.             "THAILAND" => "TH",
  232.             "TIMOR-LESTE" => "TL",
  233.             "TOGO" => "TG",
  234.             "TOKELAU" => "TK",
  235.             "TONGA" => "TO",
  236.             "TRINIDAD AND TOBAGO" => "TT",
  237.             "TUNISIA" => "TN",
  238.             "TURKEY" => "TR",
  239.             "TURKMENISTAN" => "TM",
  240.             "TURKS AND CAICOS ISLANDS" => "TC",
  241.             "TUVALU" => "TV",
  242.             "UGANDA" => "UG",
  243.             "UKRAINE" => "UA",
  244.             "UNITED ARAB EMIRATES" => "AE",
  245.             "UNITED KINGDOM" => "GB",
  246.             "UNITED STATES" => "US",
  247.             "UNITED STATES MINOR OUTLYING ISLANDS" => "UM",
  248.             "URUGUAY" => "UY",
  249.             "UZBEKISTAN" => "UZ",
  250.             "VANUATU" => "VU",
  251.             "VATICAN CITY STATE" => "VA",
  252.             "VENEZUELA, BOLIVARIAN REPUBLIC OF" => "VE",
  253.             "VIET NAM" => "VN",
  254.             "VIRGIN ISLANDS, BRITISH" => "VG",
  255.             "VIRGIN ISLANDS, U.S." => "VI",
  256.             "WALLIS AND FUTUNA" => "WF",
  257.             "WESTERN SAHARA" => "EH",
  258.             "YEMEN" => "YE",
  259.             "ZAMBIA" => "ZM",
  260.             "ZIMBABWE" => "ZW"
  261.         );
  262.  
  263.         foreach ($countries as $country => $countryAbbr) {
  264.             echo '<option value="'.$countryAbbr.'">'.ucwords(strtolower($country)).'</option>'."\n";
  265.     }
  266.  
  267.         echo '</select>'."\n";
  268.     }
  269.  
I just felt like sharing. Hope that it helps!

Regards,

Jeff
Mar 29 '09 #5
thank you for all of you for your valuable suggestions..

i am having all the values in database.. how can i retrieve it according to selection?
my question is in onchange function whether i have to pass the entire table values to the javascript function.. and have to check the dropdown-selected value there in javascript code and to populate other list boxes accordingly???

can you clarity my doubt?? thank you in advance.
Mar 29 '09 #6
numberwhun
3,509 Expert Mod 2GB
@pinocchio123
If this has turned into a javascript question that I will move this question over to there to get your question answered.

Regards,

Jeff
Mar 29 '09 #7
Amzul
130 100+
@Markus
depends i say.
if every user use this drop list for sure then i would store the info in a js file even better from db as json.
but if not
no need to send to the user 20k js file with all the info, what if he wont use it??
ajax call here is the same.
Mar 30 '09 #8
acoder
16,027 Expert Mod 8TB
@pinocchio123
If it's stored in a database, use Ajax to retrieve the corresponding state/city values. Try states to begin with and you can build on it to trigger the city drop-down change.

If the original country selection is blank, both state and city will be empty (no options). When a selection is made, make a call to a function which makes the request to you PHP page to populate the state drop-down with the correct options for the chosen country.
Mar 31 '09 #9
hi am seeking of exactly the same response. have you got the code ?...
Feb 11 '10 #10
hi am seeking exactly the same. have you got the code ?...
Nov 25 '11 #11
acoder
16,027 Expert Mod 8TB
Rather than a code request, how about making an attempt?

There's enough clues to get started even in this thread.
Nov 25 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: B. G. Mahesh | last post by:
hi In the registration form I have city, state, country fields. I was wondering if there was a database available on the net which has the list of states in each of the countries. That way when...
1
by: jonny | last post by:
Where can I find a dbase for index.PHP Country|State|Province|CITY bjve@bjve.com
11
by: Deiter | last post by:
State Machines and Coroutines The other thread on goto: Lead me to want to ask... In the spirit of state machines and coroutines, This n00b to C would like to know if setjmp/longjmp are the only...
0
by: Tim Marshall | last post by:
HI folks, I'm doing an app that requires the user to be able to choose a country, then a state/province/territory, then a city and possibly somewhere in-between, some kind of region, perhaps...
0
by: ¥øÃZ½Þ¤j·Ý | last post by:
Dear All I have a question, in US, the address should displayed as _____ (Room) ______ (Floor) ______ (Building) ______________________________ (Road) ______________________________ (District)...
10
by: somaskarthic | last post by:
Hi In my php page , there is a user registration form. Here the user has to select the country, state, city from the drop down box. How this can be handled in php? If a country is selected in a...
5
by: jakas | last post by:
Hi In my php page , there is a user registration form. Here the user has to select the country, state, city from the drop down box. How this can be handled in php? If a country is selected in a...
6
by: dkyadav80 | last post by:
Hi sir, I'm new about xml, javascript. I have two selection field(html) first is city and second is state. the city and state values should be store in xml file. when user select city then all...
7
by: tokcy | last post by:
Hi everyone, I need the world database of country state and city. Actually i have three drop down option in my project in 1st drop down country name should come from database and 2nd drop down...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.