[eluser]Unknown[/eluser]
i want to popualte two dropdown one is for compnay names and other is for model names.
actually i filled first dropdown from database.
but i want to fill second dropdown from database with onchange event on first dropdown.
Code:
this is script code
<?php //echo form_open("welcome"); ?>
<?php $cities['#'] = 'Please Select'; ?>
<label for="country">Truck </label>
<?php echo form_dropdown('mkno', $countries, '#', 'id="country"'); ?><br />
<label for="city">Model </label>
<?php echo form_dropdown('modno', $cities, '#', 'id="cities"'); ?><br />
<select id="cities" name="cities" >
<input type="submit" name="submit" value="submit" >
[removed]
$('#cities, #f_city_label').hide();
$('#country').change(function(){
var state_id = $('#country').val();
if (state_id != ""){
var post_url = "/index.php/welcome/get_cities" +state_id;
$.ajax({
type: "POST",
url: post_url,
success: function(cities) //we're calling the response json array 'cities'
{
$('cities').empty();
$('#cities, #city_label').show();
$.each(cities,function(modno,modnm)
{
var opt = $('<option />'); // here we're creating a new select option for each group
opt.val(modno);
opt.text(modnm);
$('#cities').append(opt);
});
} //end success
}); //end AJAX
} else {
$('#cities').empty();
$('#cities, #city_label').hide();
}//end if
}); //end change
[removed]
This is Model Code to Popualte second dropdown from database with selection of first dropdown
function get_cities_by_state($tree = null)
{
$this->db->select('modno,modnm');
if($tree != NULL){
$this->db->select('modno,modnm');
$this->db->where('mkno', $state);
$query = $this->db->get('mkmodel');
$cities = array();
if($query->result())
{
foreach ($query->result() as $city)
{
$cities[$city->modno] = $city->modnm;
}
return $cities;
}
else
{
return FALSE;
}
}