Country,State,City Drop Down List using AJAX

The following code will be  used to display Country, State, City menu in PHP using AJAX, based on the Country selection from drop down menu, State/Province drop down values will be populated without refresh the page and the values  in City menu will also be populated based on the State/Province drop down selection.

Registration.php

AJAX Script

function showstate(str)
{
if (str==””)
{
document.getElementById(“txtHint”).innerHTML=””;
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(“txtHintstate”).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(“GET”,”state_ajax.php?q=”+str,true);
xmlhttp.send();
}

function showcity(str)
{
if (str==””)
{
document.getElementById(“txtHint”).innerHTML=””;
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(“txtHintcity”).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(“GET”,”city_ajax.php?q=”+str,true);
xmlhttp.send();
}
Menu HTML

<form>
<select name=”country_name” onchange=”showstate(this.value)”>
<option selected=”selected” value=”0″>-Select-</option>
<option value=”INDIA”>INDIA</option>
<option value=”Sri Lanka”>Sri Lanka</option>
<option value=”USA”>USA</option>
</select></form>
</td>
<td>State:</td>
<td>
<div id=”txtHintstate”>
<select onchange=”showcity(this.value)”><option></option></select>
</div>
</td>
<td>City:</td>
<td>
<div id=”txtHintcity”>
<select>
<option selected=”selected”></option>
</select>
</div>

state_ajax.php

<?php
$q=$_GET["q"];

$con = mysql_connect(‘localhost’, ‘root’, ”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“Master_Regist”, $con);

$sql=”SELECT distinct State_name FROM Country_State_City_details WHERE Country_name = ‘”.$q.”‘”;

$result = mysql_query($sql);
?>
<select onchange=”showcity(this.value)”>
<?
while($row = mysql_fetch_array($result))
{
echo “<option>” . $row['State_name'] . “</option>”;
}
echo “</select>”;

mysql_close($con);
?>

city_ajax.php

<?php
$q=$_GET["q"];

$con = mysql_connect(‘localhost’, ‘root’, ”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“Master_Regist”, $con);

$sql=”SELECT City_name FROM Country_State_City_details WHERE State_name = ‘”.$q.”‘”;

$result = mysql_query($sql);
echo “<select>”;
while($row = mysql_fetch_array($result))
{
echo “<option>” . $row['City_name'] . “</option>”;
}
echo “</select>”;

mysql_close($con);
?>

Check it!

About these ads

About velusaravana

I am a software professional.
This entry was posted in Uncategorized. Bookmark the permalink.

22 Responses to Country,State,City Drop Down List using AJAX

  1. tahjib says:

    this is not working

  2. velusaravana says:

    Hi
    Thanks for your post. Could I know what issue you have faced?
    I can help you.
    Thanks,
    Saravanan V

  3. Shalli says:

    Is there a way to get 4 drop down boxes

    So country, region, county and city. Any help greatly appreciated

  4. Rakesh says:

    Very Nice Script

  5. atul says:

    i want to entry & update that entry using ajax????
    pls help

  6. akhilesh says:

    i have 3 drop down menu ie state, city, location. all 3 table are stored in database. on clicking value of location drop down , corresponding address from location table should be dispalyed

  7. jithin says:

    i want database structure

    • velusaravana says:

      there is no big database structure, just one table called “Country_State_City_details” which has list of countries, list of states for a country and list of cities for a state.

      • jithin says:

        hi saravana,

        i’m presently working as a trainee PHP developer.

        i’m get lot of doubt’s while practicing, so is der any idea to contact u at any time.. how can i contact u online other than this

      • velusaravana says:

        you can contact me after 8PM, no issue.. I will be available on Facebook..

  8. jithin says:

    i sent u frnd req in fb plz add me

  9. On the net this short article turn out to be very insightful, though the information is not likely alternatively newer. I happen to be your personal extended individuals at this time!You wished my own appraisal into your internet page? I only say Maintain it to remain! It is very installation for your needs. Whereas sometimes more colouring. Hey! Absolutely adore coursesmart. My buddies are pleased toooo. You best earn that Sammy’s. Superb perform plus appreciate your products I’m just Mika, brand-new there! With the help of all the best. wow, mummy, you are the very best! many thanks taking us for you to oregon. Pleasing role along with the replacing. My wife and i screen printed ones reports.Selecting the greatest valid reason move to help you Manhattan.

    louis vuitton graffiti bagReplica lv wallets for cheap

  10. Rajeshwar Bose says:

    Thank you…it worked….even i ma a php developer…can i connect you in facebook.??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s