Trying to populate a drop down list for cities once a user makes a selection for the state using Jquery/Ajax functionality
HTML Form has...
<?php $allRegions = Region::newInstance()->getStatesByCountry('US'); ?>
<select name="regionId" id="regionId">
<?php foreach($allRegions as $region) { ?>
<option value="<?php echo $region['value'] ; ?>"><?php echo $region['name'] ; ?></option>
<?php } ?>
</select>
<select name="cityId" id="cityId">Choose a city</select>
Jquery code...
$(document).ready(function(){
$("#regionId").change( function() {
var regionId = $(this).val();
var url = '<?php echo site_base_url(true)."?page=ajax&action=cities®ionId="; ?>' + regionId;
$.ajax({
type: "GET"
url: url,
dataType: 'json',
success: function(msg){
if (msg != ”){
$("#cityId").html(msg).show();
}
}
});
});
});
</script>
The issue is that on selecting a region the cities drop down select doesn't get populated ... can't figure it out.. any help is appreciated.
One problem is that you have a strange quote character:
if (msg != ”){
You probably mean this:
if (msg != ''){
and another problem is:
type: "GET"
that should be:
type: "GET",
Not to be rude, but your code is very nested, and unecessary complicated. Fix all the mentions above from the others, and also this:
<?PHP
foreach($allRegions as $region) {
<option value="<?php echo $region['value'];" . "><" . echo $region['name']; .
"</option>" } ?>
</select>