asifnayem wrote:
Hi,
I need a help regarding Auto populating the Select box upon selection. Like: I allow registration from 10 countries. Bangladesh, India, Pakistan, Japan, China.......
If any one select Bangladesh, then the next select box should show the cities of Bangladesh like dhaka, chittagong, khulna etc. and again if any one select india it should show the cities of india like: mumbai, delhi, chennai etc. and if they select the city then the next select box should show the areas name. I already have the lists of the areas on my DB.
Now I need help to do this. I can't make it by what I got from Google aunt. Please any one show me how to make it with php & jquery.
Regards.
php file to get the cities from the database
<?php
//get cities
//getcity.php
if(isset($_POST['id']))
{
$id = intval($_POST['id']);
$query = mysql_query("SELECT * FROM cities WHERE country_id='".$id."'");
if(mysql_num_rows($query)>0)
{
echo '<select name="cities" id="cities">';
while($c=mysql_fetch_object($query))
{
echo '<option value="'.$c->name.'">'.ucfirst($c->name).'</option>';
}
echo '</select>';
}
}
?>
//html document with jquery as a dependency
<html>
<head>
<title>Load select box</title>
<script type="text/javascript" src="/resources/js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#country').change(function(){
$.post('getcity.php', {id:$(this).val(), function(data){
if(data.length>0)
{
$('#cities').html(data).fadeIn('slow'); //this will show the cities for that selected country
}
});
});
});
</script>
</head>
<body>
<div id="form">
<form action="" method="post">
<select name="country" id="country">
<option value="1">India</option>
<option value="2">Bangladesh</option>
<!-- Add more here -->
</select>
<div id="cities" style="display:none">
</div>
</form>
</div>
</body>
</html>
Wapcreate.com mobile site builder, wapcreator, wapbuilder, wap developer