Hi,
I have some problem in ajax like i am not able to retrieve data through Ajax. Please look at the attachment pic to better understand the scenario.
php.JPG
I am trying to get all the values from a database which have 4 states and 3 cities in each state and third 2 or three market in each citi.
Here i am getting issue for the first time when the page is first time loaded. I have called a javascript function on first page which gets the value for state and cities is loaded but the market menu remains the blank. I want to get the value in market according the whatever city is selected on the city tab. Hope you can understand what i am looking for. I have tried to find out the problem but all in vain. Here i am attaching all the php code below and hop i need not to upload database details but if you require then i will add too. Thank in advance.
Here is the main designing file code:
Here is the Code file code:PHP Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function Action(link,md)
{
var obj,url;
url="dcode.php?mode="+md+"&id="+link+"&r="+Math.random();
try {
obj=new XMLHttpRequest();
}
catch(e)
{
try {
obj=new ActivexObject("Microsoft.XMLHTTP");
}
catch(e) {
alert(e);
}
}
obj.open("GET",url,true);
obj.send(null);
obj.onreadystatechange=function()
{
if(obj.readyState==4)
{
var res=obj.responseText;
if(md=='city')
{
document.getElementById("market").innerHTML=res;
}
else {
document.getElementById("city").innerHTML=res;
}
//document.getElementById("city").innerHTML=res;
}
}
}
</script>
<?
?>
</head>
<body>
<form action="" method="get">
<table width="200" border="1" align="center">
<tr>
<th scope="row"><label>
<select name="state" id="state" style="width:150px;" onchange="Action(this.value,'state')">
<?
mysql_connect("localhost","root","") or die("check server");
mysql_select_db("students")or die("check database");
$str="select sno,sname from state";
$rs=mysql_query($str);
while(list($scode,$stname)=mysql_fetch_array($rs))
{
echo "<option value='$scode'>$stname</option>";
}
?>
</select>
</label></th>
</tr>
<tr>
<th scope="row"><label>
<select name="city" id="city" style="width:150px;" onchange="Action(this.value,'city')">
</select>
</label></th>
</tr>
<tr>
<th scope="row"><select name="market" id="market" style="width:150px;">
</select></th>
</tr>
</table>
</form>
<script type="text/javascript">
Action(101,'state');
</script>
</body>
</html>
PHP Code:
<?
mysql_connect("localhost","root","") or die("check server");
mysql_select_db("students")or die("check database");
if($_REQUEST["mode"]==state)
{
$id=$_REQUEST["id"];
$str="SELECT citycode,cityname
FROM city
WHERE statecode
IN (
SELECT statecode
FROM city
JOIN state ON city.statecode = $id)";
$rs=mysql_query($str);
while(list($citycode,$cityname)=mysql_fetch_array($rs))
{
echo "<option value='$citycode'>$cityname</option>";
}
}
if($_REQUEST["mode"]==city)
{
$cid=$_REQUEST["id"];
$str="SELECT mcode,mname
FROM market
WHERE citicode
IN (
SELECT citicode
FROM market
JOIN city ON market.citicode = $cid)";
$rs=mysql_query($str);
while(list($mcode,$mname)=mysql_fetch_array($rs))
{
echo "<option value='$mcode'>$mname</option>";
}
}
?>
Submit Your Article
Forum Rules

Reply With Quote
