Country State City Dropdown in jQuery Ajax PHP & MySql

This is a very simple html script to add city, state and country list of world in your website without any extra effort, Just copy and paste below html code in your website.

Note: Please don’t change select box id and class name otherwise script will not work.




<select name="country" class="countries" id="countryId">
<option value="">Select Country</option>
</select>
<select name="state" class="states" id="stateId">
<option value="">Select State</option>
</select>
<select name="city" class="cities" id="cityId">
<option value="">Select City</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>



Β 

10+ FREE PHP MySql City State Country Dropdown

Following are the list of popular top rated hand picked PHP MySql City State Country Dropdown.

I Hope you liked Hand-picked list of PHP MySql City State Country Dropdown, Don’t forget to Subscribe My Public Notebook for more useful Hand-picked PHP and MySql code examples, tutorials and articles.

64 Replies to “Country State City Dropdown in jQuery Ajax PHP & MySql”

  1. hai, This script very helpful…. Thank u…
    how to get database php and ajax based dropdown list of country, state and city. please help me

  2. Hello Rohit,

    Finally I found the code for Country, State and City, Its great example. Thank you very much for sharing.

    I am facing a problem in this, and the problem is how can I show selected Item after form submission.

  3. Hi shikha,

    After form submission You can get the id of selected item and can use that id to get corresponding city state country details using jquery.

    2nd option is you can download my county state city database dump and import it in your local database after that download source code by clicking on download example button and configure it in your project.

  4. How to insert two type multiple values selected in checkbox in database ,
    <?php
    $checkbox1 = $_POST['batch'];
    $checkbox2 = $_POST['particular_name'];

    if(@isset($_REQUEST["sub"]))
    {

    for ($i=0; $i<sizeof($checkbox1);$i++) {
    for ($j=0; $j
    its not workin pls help me

  5. I am not able to understand what you are going to do please describe more as i understand so far you can try this…

    <?php
      for ($i=0; $i<sizeof($checkbox1);$i++) {
           $check1 = $checkbox1[$i];
           $check2 = $checkbox2[$i];
         INSET INTO TABLE_NAME (CHECKBOX1, CHECKBOX2) VALUES ('$check1', '$check2');
      }
    ?>
  6. Hello Rohit..
    This is great script,but if i want to show and use only the States and City(i.e. according to selected State) of India…Then how can i achieve this..
    Please help me….

  7. Thanks for reply..
    But if i only call States and City list, then dropdown list of state will not open…and also city’s dropdown list is also not opened.
    I think city’s dropdown list is depend on State’s dropdown list(or State’s ID ) and State’s dropdown list is depend on Country’s dropdown list(or Country’s ID)..

    Would you please give me source code for this …I just want to show Sates and City of India…

  8. Hi Gopal,

    Use below script to fetch states and cities list of india.
    Click Here For Demo

    <select name="state" class="states" id="stateId">
    <option value="">Select State</option>
    </select>
    <select name="city" class="cities" id="cityId">
    <option value="">Select City</option>
    </select>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://lab.iamrohit.in/js/location.js"></script>
    <script>
    $(function() {
      var loc = new locationInfo();
          loc.getStates(101);
    });
    </script>
  9. Your script is very good. I just needed a help from you. I need only Country and City. I can I achieve it. Can you please provide a code for that.

    Thanks

  10. You can achieve it but you have to do little effort, Download full script from my github repository with database and map country id directly in city table corresponding states, Then you can fetch cities directly by passing their county id.

    Thanks,
    Rohit

  11. Hi,
    Thanks for your post. For edit screen how can we bind country/state/city from database to ui as selected value.. please help me
    Thanks in advance

  12. When we change the url from this to the local url it’s not working. please help me.

    var rootUrl = “http://lab.iamrohit.in/php_ajax_country_state_city_dropdown/api.php”;

  13. Please check your php vesion script support php 5.5+ because i used finally keyword for exceptional handling in the location.php classs file.

    If still you are facing any issue please let me know

  14. I have some updates for your codes.
    First in dbconfig.php:
    insert
    mysqli_set_charset($link,”utf8″);
    after
    $link = mysqli_connect(self::$host, self::$username, self::$password, self::$dbname);

    Here in Brazil many cities got accent on names and it returns fatal error on page because of that. Setting utf8 it is solved.

    Now in location.php I added “ORDER BY name ASC” at the end of all querys because I updated the database. Your DB is good but there aren’t 10% of Brazil cities. BUT it is not returing ordered by name anyway. Can you help with it? Thank you, keep working, You are amazing.

  15. Hi Rohit,
    I have used your code in one of my project and it is working fine in that as the form has only one address field.

    For one of my other project I have 3 address fields – permanent add, present add and employer’s add. For each field I have Country State and City. But your code is not working if I have more than one address fields. I have set unique ids for all the fields.

    Your suggestions and help will be highly appreciated in solving this issue.

    Thanks in Advance.

  16. I have downloaded the zip file that contains only SQL files. Could you please provide me the link of the php files as well ?

  17. I want country state city drop down in cake php.
    May it’s possible on cakephp if Yes so help me

  18. You need to download full source code and need to customize as per cakephp rules, Javascript part remains same, You have to create models for city state and country and write functions to fetch list view of data.

  19. How to post value of state or city name using the option value attribute

    i got value as number ,how to change it to name with respected field
    please help i need to store value in database with value name and also send mail the contain

    thanks in advanced

  20. in india as selection for country the state name show vaishali,natwar ,narora…i think these are not the state of india..how can i get rid from it.

  21. hi sir,

    as a fresher its very useful code for me.but am using Java language and its work in my project.but the country code is not display when our internet connection slow..how can I list this country names if I don’t have a internet connection??

  22. Hi Rohit

    Please I need to store the ‘name’ of my selection and not its ‘id’ . .

    I got the ‘state’ to post names to db but the country continues to store ‘id’ in db . .

    Please help

    From South Africa

    Thanx !!

  23. Hi Rohit, thanks for your code, it’s really helpful, but I think the Country-state dropdown values are odd, I got a ton of very strange options in the states drop-down of the US, can you check it.

  24. My php script not storing name of country and state in database its storing their id.whats the exact problem i use name tag in html as well.

  25. i already downloaded full source code. but still when i m trying to change rootURL in Location.JS file it shows an error code

  26. Helloo Rohit,
    The source code was really helpful..
    Awesome..
    Optimized Code which helped to reduce the webpage code…

  27. hi bro, thanks for your script.
    I want to fetch all cities in India.
    How I can achieve this??

  28. Use below script to fetch states and cities list of india.
    Click Here For Demo

    option value="">Select State</option>
    </select>
    <select name="city" class="cities" id="cityId">
    <option value="">Select City</option>
    </select>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://lab.iamrohit.in/js/location.js"></script>
    <script>
    $(function() {
      var loc = new locationInfo();
          loc.getStates(101);
    });
    </script>
  29. Thank you so much Rohir sir,
    for sharing such a smart code.
    you made my work simple.
    thanks a tonn!!

  30. Hi,
    When I select State with single word like ‘Eastern’ then I get all cities. but when I select state with more than one word like ‘Swains Island’ or ‘Burj bu Arriji’ then I don’t get cities at all.
    But this is not a problem with ‘India’

  31. hello,
    thank you very much for this script. Its very usefully for me. How can I list after insert it in another table. For example I inserted country 3 state 5 city 6 with another information for user, How can I call it inserted data from mysql with you script so that user change it again.

  32. Hi, Kindly help.
    I want to select the country, state and city from a drop down menu like yours which I can do but I want to learn how to add a community. for example, community A under Country 1, State 1 and city 1 while community B for country 1, state 2 and city 5.

    I really want to know how to insert value from each of these drop down into another table

  33. Nice Solution brother with error handling. It’s very good example every one can use this solutions for big projects also. Very accurate solution . (y)

  34. when i am inserting the country,state,city, in my database.each and every time value is inserting (like 23,2).so can you help me rohit how can fix this problem

  35. your code running properly for 6 months. now it’s show error:0; error found, error message: error.. how can i fix this problem?

  36. Hi Rohit,
    I used your code a few months ago to create my own web service for dynamic dropdowns. I set it up on a wizard at http://geodata.solutions and it allows users to filter and order, and group by continent, test their dropdowns and then just paste a bit of code into their site.
    It might be a good solution for those who were using your service to switch to mine?
    I hope to see some of you very soon, and get in touch if you have any questions about geodata.solutions

  37. Hi Rohit, Great script but I am not able to figure out how to default select values in dropdown based on database values.
    I am getting DB values in a variable, but not able to get how to set the dropdown based on it.

  38. Hi Rohith your code helps me a lot
    ive integrated states and cities in my project
    but after submitting i cant get the form dropdown value to another page using $_POST[‘state’]
    i want state name
    but i am getting numeric values
    please help me

  39. hi i posted yesterday and you deleted it without responding to it! Why am i getting a 404 error when using your script?

  40. Hi, Thanks for the script. Works nicely! But i’d like to know from where you got the data? GeoNames.org? If, yes, how did you do it? Just trying to find a way to keep the database updated just incase if you do not plan to keep the database updated.

    Thanks again πŸ™‚

  41. i rohit its very good api from your side
    but i have use this every time else condtion fetch message country fetched sucessfully and country not append with select box
    please tell about that what the basic reason for this alert ..

  42. Rohit, thank you very much for this code. It has been really helpful. However, when I select United States as Country and New Jersey or New Hampshire the cities do not come sorted by name. How can I solve this? I tried adding ORDER BY name at the end and it still does not work. Any help will be appreciated.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.