Hi...
After long time I came back.

Actually I am trying to display State and City list in 2 drop down box using PHP and Ajax. State and city list is coming from Mysql.
I am able to get the list of state but after changing the state city drop down box in not getting populated by City list. I am using 2 PHP file test.php and show_city.php. Online Demo Problem
Here is the code -

<?php
$host="localhost"; // Host name 
$username="cbsecpsn_vishal"; // Mysql username 
$password="most^^@anted123"; // Mysql password 
$db_name="cbsecpsn_cbsecsnip"; // Database name 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

?>
<html>
<head>
<script type="text/javascript">
// JavaScript Document
var XMLHttpRequestObject=false;
function display(state_id){
   if(window.XMLHttpRequest){
      XMLHttpRequestObject=new XMLHttpRequest();
   }
   else if(window.ActiveXObject){
      XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
   } 
   XMLHttpRequestObject.onreadystatechange=function(){
     if (XMLHttpRequestObject.readyState==4 && XMLHttpRequestObject.status==200)
     {
         document.getElementById("show_city").innerHTML=XMLHttpRequestObject.responseText;
     }
   }
XMLHttpRequestObject.open("GET","show_city.php?state_id="+state_id,true);
XMLHttpRequestObject.send();
}
</script>
</head>
<body>
<form>
<select name="state" onChange="display(this.value)">
<option value="" selected="selected">-- Select state --</option>
<?php
$query="select * from tbl_state";
$query_result=mysql_query($query)or mysql_error();
while($row=mysql_fetch_array($query_result)){
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['state_name']; ?></option>
<?php
}
?>
</select>
</form>
<br />
<div id="show_city">
<select name="city">
<option value="" selected="selected">-- Select city --</option>
</select>
</div>
</body>
</html>

show_city.php

<?php
$host="localhost"; // Host name 
$username="cbsecpsn_vishal"; // Mysql username 
$password="most^^@anted123"; // Mysql password 
$db_name="cbsecpsn_cbsecsnip"; // Database name 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$state_id=$_REQUEST['state_id'];

$query="select * from tbl_city where state_id='$state_id'";

?>
<select name="city">
<option value="" selected="selected">-- Select city --</option>
<?php
$query_result=mysql_query($query)or mysql_error();
while($row=mysql_fetch_array($query_result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['city_name']; ?></option>
<?php
}
?>
</select>

Database

CREATE TABLE IF NOT EXISTS `tbl_city` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `state_id` varchar(6) NOT NULL,
  `city_name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `tbl_state` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `state_name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1; 

Please guide me where I am doing mistake...!

Well on the form part on the first set of code, i changed the php code so i echos all variable into a select element, rather than what you had. Your method make it echo all variables into 1 select element.

<form>
<select name="state" onChange="display(this.value)">
<option value="">-- Select state --</option>
<?php
$query="select * from tbl_state";
$query_result=mysql_query($query)or mysql_error();
while($row=mysql_fetch_array($query_result)){
echo "<option value='".$row['id']."'>".$row['state_name']."</option>";
}
?>
</select>
</form>

On the show_city.php, instead of using $_REQUEST, try using $_GET. Now to test this, try just echoing the results rather than puttin them into a form

<?php
$host="localhost"; // Host name
$username="cbsecpsn_vishal"; // Mysql username
$password="most^^@anted123"; // Mysql password
$db_name="cbsecpsn_cbsecsnip"; // Database name

$state_id=$_GET['state_id'];

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$query="select * from tbl_city where state_id='$state_id'";
$query_result=mysql_query($query)or mysql_error();
while($row=mysql_fetch_array($query_result)){
echo $row['id'];
}
?>

test that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.