vishalonne
Technical User
- Jul 29, 2012
- 49
Hi...
After long time I came back.
Actually I am trying to display State and City lsit 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
Here is the code -
show_city.php
Please guide me where I am doing mistake...!
After long time I came back.
Actually I am trying to display State and City lsit 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
Here is the code -
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");
?>
<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>
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>
SQL:
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...!