This is a country dowp down list. Its works fine. The States are related to the countries and cities are related to the States. I am getting one problem for example if I have already select the country,state, and city. and after if I change the country again the state will changed like 'select your state' but the city will not. and I want to also initilized the city like 'Select your city'. Plesae help me.
I am using the populate country file with ajax.js
My coding is as below:
populate_country.php
<?php
include "connection.php";
if (isset($_GET['country']))
{
$query="select co_id,co_name from tblcountry order by co_name";
if (!($result=mysql_query($query,$objdbConnect->con))) die(mysql_error());
echo "<select name='country' onchange='PopulateState(this.value)'>";
echo "<option>Select your Country</option>";
if (mysql_num_rows($result))
{
while(list($id, $name)=mysql_fetch_array($result))
{
echo "<option value=$id>$name</option> " ;
}
echo "</select>";
}
}
if (isset($_GET['state']))
{
$MyId=$_GET['q'];
if ($MyId==""){return;}
$query="select st_id,st_name from tblstate where st_co_id='$MyId' order by st_name";
if (!($result=mysql_query($query,$objdbConnect->con))) die(mysql_error());
echo "<select name='state' onchange='PopulateCity(this.value)'>";
echo "<option>Select your State</option>";
if (mysql_num_rows($result))
{
while(list($id, $name)=mysql_fetch_array($result))
{
echo "<option value=$id>$name</option> ";
}
echo "</select>";
}
}
if (isset($_GET['city']))
{
$MyId=$_GET['q'];
if ($MyId==""){return;}
$query="select ci_id,ci_name from tblcity where ci_st_id='$MyId' order by ci_name";
if (!($result=mysql_query($query,$objdbConnect->con))) die(mysql_error());
echo "<select name='city'>";
echo "<option>Select your City</option>";
if (mysql_num_rows($result))
{
while(list($id, $name)=mysql_fetch_array($result))
{
echo "<option value=$id>$name</option> " ;
}
echo "</select>";
}
}
mysql_close($objdbConnect->con);
?>
AJAX.js
function PopulateCountry()
{
xmlhttp=GetXMLHttpObject();
if (xmlhttp==null)
{
alert("Your Browser deos not Support XMLHTTP");
}
else
{
var url="populate_country.php";
url=url + "?q=" + "&country";
url=url + "&sid=" + Math.random();
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=stateChangedCountry;
}
}
function stateChangedCountry()
{
if (xmlhttp.readyState==4 )
{
document.getElementById("country").innerHTML=xmlhttp.responseText;
}
}
//------------------------------------------------------------------------------------------
function PopulateState(str)
{
if (str.lentgth == 0)
{
return document.getElementById("state").innerHTML="";
}
xmlhttp=GetXMLHttpObject();
if (xmlhttp==null)
{
alert("Your Browser deos not Support XMLHTTP");
}
else
{
var url="populate_country.php";
// url=url + "?q=" + str;
url=url + "?q=" + str + "&state";
url=url + "&sid=" + Math.random();
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=stateChangedState;
}
}
function stateChangedState()
{
if (xmlhttp.readyState==4 )
{
document.getElementById("state").innerHTML=xmlhttp.responseText;
}
}
//------------------------------------------------------------------------------------------
function PopulateCity(str)
{
if (str.lentgth == 0)
{
return document.getElementById("city").innerHTML="";
}
xmlhttp=GetXMLHttpObject();
if (xmlhttp==null)
{
alert("Your Browser deos not Support XMLHTTP");
}
else
{
var url="populate_country.php";
url=url + "?q=" + str + "&city";
url=url + "&sid=" + Math.random();
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=stateChangedCity;
}
}
function stateChangedCity()
{
if (xmlhttp.readyState==4 )
{
document.getElementById("city").innerHTML=xmlhttp.responseText;
}
}
function GetXMLHttpObject()
{
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
}