How I can write down a code which uses two Two AJAX-es in combo-boxes?
For Country, when user clicks Country combo-box, State Combo-box will fetch with respective Country, and if when user clicks State combo-box, cities Combo-box will fetch with respective State. All data will came from database. I successfully populate for the Country but not for State

Recommended Answers

Something like this example? You can take the ajax code from the page if you need it.

Jump to Post

All 3 Replies

Something like this example? You can take the ajax code from the page if you need it.

try to use jQuery

$("#id of of box when you click will do this function").click(function(){
$.get('getcountry.php', function(data) {
	$('# id of your box where you want to display the result').html(data);
});
});

in youre getcountry.php

$getCountry = mysql_query("select Name from Country");
		$gco = '<option value="">Select Country </option>';
		while($CountryName = mysql_fetch_array($getCountry)){
		$gco .='<option value="'.$CountryName['Name'].'">'.$CountryName['Name'].'</option>';
	}
	echo $gco;
<select name="country" id="country"></select>
<select name="state" id="state"></select>
<select name="cities" id="cities"></select>

$("#country").click(function(){
	$.get('getstate.php', function(data) {
	$('#state).html(data);
	});
});

$("#state").click(function(){
	$.get('getcities.php', function(data) {
	$('#cities).html(data);
	});
})

getstate.php

$getstate = mysql_query("select state from table where country='".$_POST['country']."'");
		while($StateName = mysql_fetch_array($getstate)){
		echo '<option value="'.$StateName['state'].'">'.$StateName['state'].'</option>';
	}

getcities.php

$getcities = mysql_query("select cities from table where state='".$_POST['state']."'");
		while($CitiesName = mysql_fetch_array($getcities)){
		echo '<option value="'.$CitiesName['cities'].'">'.$CitiesName['cities'].'</option>';
}

and dont forget to download http://code.jquery.com/jquery-1.5.1.min.js

Be a part of the DaniWeb community

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