country,state,city selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pinocchio123
    New Member
    • Mar 2009
    • 6

    country,state,city selection

    hi friends...

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

    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...
  • Amzul
    New Member
    • Oct 2007
    • 130

    #2
    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

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      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.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          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:

          Code:
              function state_selection() {
          	echo '<select name="state">'."\n";
          	
                  $states = array(
                      'Alabama' => 'AL',
                      'Alaska' => 'AK',
                      'American Samoa' => 'AS',
                      'Arizona' => 'AZ',
                      'Arkansas' => 'AR',
                      'California' => 'CA',
                      'Colorado' => 'CO',
                      'Connecticut' => 'CT',
                      'Delaware' => 'DE',
                      'District of Columbia' => 'DC',
                      'Federated States Of Micronesia' => 'FM',
                      'Florida' => 'FL',
                      'Georgia' => 'GA',
                      'Hawaii' => 'HI',
                      'Idaho' => 'ID',
                      'Illinois' => 'IL',
                      'Indiana' => 'IN',
                      'Iowa' => 'IA',
                      'Kansas' => 'KS',
                      'Kentucky' => 'KY',
                      'Louisiana' => 'LA',
                      'Maine' => 'ME',
                      'Marshall Islands' => 'MH',
                      'Maryland' => 'MD',
                      'Massachusetts' => 'MA',
                      'Michigan' => 'MI',
                      'Minnesota' => 'MN',
                      'Mississippi' => 'MS',
                      'Missouri' => 'MO',
                      'Montana' => 'MT',
                      'Nebraska' => 'NE',
                      'Nevada' => 'NV',
                      'New Hampshire' => 'NH',
                      'New Jersey' => 'NJ',
                      'New Mexico' => 'NM',
                      'New York' => 'NY',
                      'North Carolina' => 'NC',
                      'North Dakota' => 'ND',
                      'Northern Mariana Islands' => 'MP',
                      'Ohio' => 'OH',
                      'Oklahoma' => 'OK',
                      'Oregon' => 'OR',
                      'Palau' => 'PW',
                      'Pennsylvania' => 'PA',
                      'Puerto Rico' => 'PR',
                      'Rhode Island' => 'RI',
                      'South Carolina' => 'SC',
                      'South Dakota' => 'SD',
                      'Tennessee' => 'TN',
                      'Texas' => 'TX',
                      'Utah' => 'UT',
                      'Vermont' => 'VT',
                      'Virgin Islands' => 'VI',
                      'Virginia' => 'VA',
                      'Washington' => 'WA',
                      'West Virginia' => 'WV',
                      'Wisconsin' => 'WI',
                      'Wyoming' => 'WY'
          	);
          	
                  foreach ($states as $state_name => $state_abbr) {
                      echo '<option value="'.$state_abbr.'">'.$state_name.'</option>'."\n";
          	}
          	
                  echo '</select>'."\n";
              }
          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:

          Code:
              function country_selection() {
                  echo '<select name="country">'."\n";
                  
                  $countries = array(
                      "AFGHANISTAN" => "AF",
                      "Ã…LAND_ISLANDS" => "AX",
                      "ALBANIA" => "AL",
                      "ALGERIA" => "DZ",
                      "AMERICAN_SAMOA" => "AS",
                      "ANDORRA" => "AD",
                      "ANGOLA" => "AO",
                      "ANGUILLA" => "AI",
                      "ANTARCTICA" => "AQ",
                      "ANTIGUA_AND_BARBUDA" => "AG",
                      "ARGENTINA" => "AR",
                      "ARMENIA" => "AM",
                      "ARUBA" => "AW",
                      "AUSTRALIA" => "AU",
                      "AUSTRIA" => "AT",
                      "AZERBAIJAN" => "AZ",
                      "BAHAMAS" => "BS",
                      "BAHRAIN" => "BH",
                      "BANGLADESH" => "BD",
                      "BARBADOS" => "BB",
                      "BELARUS" => "BY",
                      "BELGIUM" => "BE",
                      "BELIZE" => "BZ",
                      "BENIN"  => "BJ",
                      "BERMUDA" => "BM",
                      "BHUTAN" => "BT",
                      "BOLIVIA" => "BO",
                      "BOSNIA AND HERZEGOVINA" =>  "BA",
                      "BOTSWANA" => "BW",
                      "BOUVET ISLAND" => "BV",
                      "BRAZIL" => "BR",
                      "BRITISH INDIAN OCEAN TERRITORY" => "IO",
                      "BRUNEI DARUSSALAM" => "BN",
                      "BULGARIA" => "BG",
                      "BURKINA FASO" => "BF",
                      "BURUNDI" => "BI",
                      "CAMBODIA" => "KH",
                      "CAMEROON" => "CM",
                      "CANADA" => "CA",
                      "CAPE VERDE" => "CV",
                      "CAYMAN ISLANDS" => "KY",
                      "CENTRAL AFRICAN REPUBLIC" => "CF",
                      "CHAD" => "TD",
                      "CHILE" => "CL",
                      "CHINA" => "CN",
                      "CHRISTMAS ISLAND" => "CX",
                      "COCOS (KEELING) ISLANDS" => "CC",
                      "COLOMBIA" => "CO",
                      "COMOROS" => "KM",
                      "CONGO" => "CG",
                      "CONGO, THE DEMOCRATIC REPUBLIC OF THE" => "CD",
                      "COOK ISLANDS" => "CK",
                      "COSTA RICA" => "CR",
                      "CÔTE D'IVOIRE" => "CI",
                      "CROATIA" => "HR",
                      "CUBA" => "CU",
                      "CYPRUS" => "CY",
                      "CZECH REPUBLIC" => "CZ",
                      "DENMARK" => "DK",
                      "DJIBOUTI" => "DJ",
                      "DOMINICA" => "DM",
                      "DOMINICAN REPUBLIC" => "DO",
                      "ECUADOR" => "EC",
                      "EGYPT" => "EG",
                      "EL SALVADOR" => "SV",
                      "EQUATORIAL GUINEA" => "GQ",
                      "ERITREA" => "ER",
                      "ESTONIA" => "EE",
                      "ETHIOPIA" => "ET",
                      "FALKLAND ISLANDS (MALVINAS)" => "FK",
                      "FAROE ISLANDS" => "FO",
                      "FIJI" => "FJ",
                      "FINLAND" => "FI",
                      "FRANCE" => "FR",
                      "FRENCH GUIANA" => "GF",
                      "FRENCH POLYNESIA" => "PF",
                      "FRENCH SOUTHERN TERRITORIES" => "TF",
                      "GABON" => "GA",
                      "GAMBIA" => "GM",
                      "GEORGIA" => "GE",
                      "GERMANY" => "DE",
                      "GHANA" => "GH",
                      "GIBRALTAR" => "GI",
                      "GREECE" => "GR",
                      "GREENLAND" => "GL",
                      "GRENADA" => "GD",
                      "GUADELOUPE" => "GP",
                      "GUAM" => "GU",
                      "GUATEMALA" => "GT",
                      "GUERNSEY" => "GG",
                      "GUINEA" => "GN",
                      "GUINEA-BISSAU" => "GW",
                      "GUYANA" => "GY",
                      "HAITI" => "HT",
                      "HEARD ISLAND AND MCDONALD ISLANDS" => "HM",
                      "HOLY SEE (VATICAN CITY STATE)" => "VA",
                      "HONDURAS" => "HN",
                      "HONG KONG" => "HK",
                      "HUNGARY" => "HU",
                      "ICELAND" => "IS",
                      "INDIA" => "IN",
                      "INDONESIA" => "ID",
                      "IRAN, ISLAMIC REPUBLIC OF" => "IR",
                      "IRAQ" => "IQ",
                      "IRELAND" => "IE",
                      "ISLE OF MAN" => "IM",
                      "ISRAEL" => "IL",
                      "ITALY" => "IT",
                      "JAMAICA" => "JM",
                      "JAPAN" => "JP",
                      "JERSEY" => "JE",
                      "JORDAN" => "JO",
                      "KAZAKHSTAN" => "KZ",
                      "KENYA" => "KE",
                      "KIRIBATI" => "KI",
                      "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF" => "KP",
                      "KOREA, REPUBLIC OF" => "KR",
                      "KUWAIT" => "KW",
                      "KYRGYZSTAN" => "KG",
                      "LAO PEOPLE'S DEMOCRATIC REPUBLIC" => "LA",
                      "LATVIA" => "LV",
                      "LEBANON" => "LB",
                      "LESOTHO" => "LS",
                      "LIBERIA" => "LR",
                      "LIBYAN ARAB JAMAHIRIYA" => "LY",
                      "LIECHTENSTEIN" => "LI",
                      "LITHUANIA" => "LT",
                      "LUXEMBOURG" => "LU",
                      "LAO PEOPLE'S DEMOCRATIC REPUBLIC" => "LA",
                      "LATVIA" => "LV",
                      "LEBANON" => "LB",
                      "LESOTHO" => "LS",
                      "LIBERIA" => "LR",
                      "LIBYAN ARAB JAMAHIRIYA" => "LY",
                      "LIECHTENSTEIN" => "LI",
                      "LITHUANIA" => "LT",
                      "LUXEMBOURG" => "LU",
                      "MACAO" => "MO",
                      "MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF" => "MK",
                      "MADAGASCAR" => "MG",
                      "MALAWI" => "MW",
                      "MALAYSIA" => "MY",
                      "MALDIVES" => "MV",
                      "MALI" => "ML",
                      "MALTA" => "MT",
                      "MARSHALL ISLANDS" => "MH",
                      "MARTINIQUE" => "MQ",
                      "MAURITANIA" => "MR",
                      "MAURITIUS" => "MU",
                      "MAYOTTE" => "YT",
                      "MEXICO" => "MX",
                      "MICRONESIA, FEDERATED STATES OF" => "FM",
                      "MOLDOVA, REPUBLIC OF" => "MD",
                      "MONACO" => "MC",
                      "MONGOLIA" => "MN",
                      "MONTENEGRO" => "ME",
                      "MONTSERRAT" => "MS",
                      "MOROCCO" => "MA",
                      "MOZAMBIQUE" => "MZ",
                      "MYANMAR" => "MM",
                      "NAMIBIA" => "NA",
                      "NAURU" => "NR",
                      "NEPAL" => "NP",
                      "NETHERLANDS" => "NL",
                      "NETHERLANDS ANTILLES" => "AN",
                      "NEW CALEDONIA" => "NC",
                      "NEW ZEALAND" => "NZ",
                      "NICARAGUA" => "NI",
                      "NIGER" => "NE",
                      "NIGERIA" => "NG",
                      "NIUE" => "NU",
                      "NORFOLK ISLAND" => "NF",
                      "NORTHERN MARIANA ISLANDS" => "MP",
                      "NORWAY" => "NO",
                      "OMAN" => "OM",
                      "PAKISTAN" => "PK",
                      "PALAU" => "PW",
                      "PALESTINIAN TERRITORY, OCCUPIED" => "PS",
                      "PANAMA" => "PA",
                      "PAPUA NEW GUINEA" => "PG",
                      "PARAGUAY" => "PY",
                      "PERU" => "PE",
                      "PHILIPPINES" => "PH",
                      "PITCAIRN" => "PN",
                      "POLAND" => "PL",
                      "PORTUGAL" => "PT",
                      "PUERTO RICO" => "PR",
                      "QATAR" => "QA",
                      "RÉUNION" => "RE",
                      "ROMANIA" => "RO",
                      "RUSSIAN FEDERATION" => "RU",
                      "RWANDA" => "RW",
                      "SAINT BARTHÉLEMY" => "BL",
                      "SAINT HELENA" => "SH",
                      "SAINT KITTS AND NEVIS" => "KN",
                      "SAINT LUCIA" => "LC",
                      "SAINT MARTIN" => "MF",
                      "SAINT PIERRE AND MIQUELON" => "PM",
                      "SAINT VINCENT AND THE GRENADINES" => "VC",
                      "SAMOA" => "WS",
                      "SAN MARINO" => "SM",
                      "SAO TOME AND PRINCIPE" => "ST",
                      "SAUDI ARABIA" => "SA",
                      "SENEGAL" => "SN",
                      "SERBIA" => "RS",
                      "SEYCHELLES" => "SC",
                      "SIERRA LEONE" => "SL",
                      "SINGAPORE" => "SG",
                      "SLOVAKIA" => "SK",
                      "SLOVENIA" => "SI",
                      "SOLOMON ISLANDS" => "SB",
                      "SOMALIA" => "SO",
                      "SOUTH AFRICA" => "ZA",
                      "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS" => "GS",
                      "SPAIN" => "ES",
                      "SRI LANKA" => "LK",
                      "SUDAN" => "SD",
                      "SURINAME" => "SR",
                      "SVALBARD AND JAN MAYEN" => "SJ",
                      "SWAZILAND" => "SZ",
                      "SWEDEN" => "SE",
                      "SWITZERLAND" => "CH",
                      "SYRIAN ARAB REPUBLIC" => "SY",
                      "TAIWAN, PROVINCE OF CHINA" => "TW",
                      "TAJIKISTAN" => "TJ",
                      "TANZANIA, UNITED REPUBLIC OF" => "TZ",
                      "THAILAND" => "TH",
                      "TIMOR-LESTE" => "TL",
                      "TOGO" => "TG",
                      "TOKELAU" => "TK",
                      "TONGA" => "TO",
                      "TRINIDAD AND TOBAGO" => "TT",
                      "TUNISIA" => "TN",
                      "TURKEY" => "TR",
                      "TURKMENISTAN" => "TM",
                      "TURKS AND CAICOS ISLANDS" => "TC",
                      "TUVALU" => "TV",
                      "UGANDA" => "UG",
                      "UKRAINE" => "UA",
                      "UNITED ARAB EMIRATES" => "AE",
                      "UNITED KINGDOM" => "GB",
                      "UNITED STATES" => "US",
                      "UNITED STATES MINOR OUTLYING ISLANDS" => "UM",
                      "URUGUAY" => "UY",
                      "UZBEKISTAN" => "UZ",
                      "VANUATU" => "VU",
                      "VATICAN CITY STATE" => "VA",
                      "VENEZUELA, BOLIVARIAN REPUBLIC OF" => "VE",
                      "VIET NAM" => "VN",
                      "VIRGIN ISLANDS, BRITISH" => "VG",
                      "VIRGIN ISLANDS, U.S." => "VI",
                      "WALLIS AND FUTUNA" => "WF",
                      "WESTERN SAHARA" => "EH",
                      "YEMEN" => "YE",
                      "ZAMBIA" => "ZM",
                      "ZIMBABWE" => "ZW"
                  );
                  
                  foreach ($countries as $country => $countryAbbr) {
                      echo '<option value="'.$countryAbbr.'">'.ucwords(strtolower($country)).'</option>'."\n";
          	}
          	
                  echo '</select>'."\n";
              }
          I just felt like sharing. Hope that it helps!

          Regards,

          Jeff

          Comment

          • pinocchio123
            New Member
            • Mar 2009
            • 6

            #6
            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.

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by pinocchio123
              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.
              If this has turned into a javascript question that I will move this question over to there to get your question answered.

              Regards,

              Jeff

              Comment

              • Amzul
                New Member
                • Oct 2007
                • 130

                #8
                Originally posted by Markus
                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.
                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.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by pinocchio123
                  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???
                  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.

                  Comment

                  • iSoftech
                    New Member
                    • Jul 2008
                    • 1

                    #10
                    hi am seeking of exactly the same response. have you got the code ?...

                    Comment

                    • manojdv
                      New Member
                      • Nov 2011
                      • 2

                      #11
                      hi am seeking exactly the same. have you got the code ?...

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Rather than a code request, how about making an attempt?

                        There's enough clues to get started even in this thread.

                        Comment

                        Working...