Monday, 20 July 2015

How to Dropdown country State Example

show-country-city-ajax.php
===============================
<?php
include('connection.php');
?>
<html>
<head>
<script>

function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get-data.php?q="+str,true);
//xmlhttp.open("GET","get-data1.php?yy="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

Select Your Country<select onChange="showUser(this.value)">
      <option>select</option>
<?php
$sql="SELECT * FROM country";
$result = mysqli_query($dd,$sql);
while($row = mysqli_fetch_array($result))
{
$id=$row['cid'];
echo "<option value='$id'>" . $row['coname'] . "</option>";
}
?>
</select>

<div id="txtHint" style="width:100px; border:0px solid gray;">
<b>your city disp here</b>
</div>

</body>
</html>

================
Get_data.php
===================

<?php
include('connection.php');
$q=$_GET["q"];
$sql="SELECT * FROM state WHERE conid ='$q'";
$result = mysqli_query($dd,$sql);
echo "Your State <select>";
while($row = mysqli_fetch_array($result))
{
    $id11=$row['stid'];
echo "<option>" . $row['stname'] . "</option>";
}

echo "</select>";
?>

===========
Note
===========
database
country filed  id and countryname
state filed Stid,Sname,cid(country table id)

No comments:

Post a Comment