Country State City Flow In Codeigniter Using Jquery Ajax
My Controller's Function Code:-
//STATE
public function state($id)
{
$this->load->model('admin/model');
$where=array('country_id'=>$id);
//print_r ($where); break;
$state=$this->model->sel_where('state',$where);
echo '<option>-State-</option>';
foreach($state as $row)
{
echo'<option value=''.$row['state_id'].''>'.$row['state_name'].'</option>';
}
}
//city
public function city($id)
{
$this->load->model('admin/model');
$where=array('state_id'=>$id);
//print_r ($where); break;
$city=$this->model->sel_where('city',$where);
echo '<option>-City-</option>';
foreach($city as $ct)
{
echo'<option value=''.$ct['city_id'].''>'.$ct['city_name'].'</option>';
}
}
My Model's Function Code:-
//select data with id
public function sel_where($tbl,$where)
{
$r=$this->db->get_where($tbl,$where);
$rr= $r->result_array();
//print_r($rr);break;
return $rr;
}
My View's Html Code:-
<script src='https://code.jquery.com/jquery-1.9.1.min.js' type='text/javascript'></script>
<script>
var state,city;
function change_state()
{
state=$('#country_id').val();
$('#state_id').load('<?php echo base_url();?>index.php/admin/user/state/'+state);
}
function change_city()
{
city=$('#state_id').val();
$('#city_id').load('<?php echo base_url();?>index.php/admin/user/city/'+city);
}
</script>
<label>Select Country</label>
<select id='country_id' name='country' onChange='change_state();'>
<option >Country</option>
<?php foreach($cnt as $c)
{ ?>
<option value='<?php echo $c['country_id'];?>'><?php echo $c['country_name'];?></option> <?php }?> </select>
<!--State-->
<label>Select State</label>
<select id='state_id' name='state' onChange='change_city();'>
<option >-State-</option>
</select>
<!--City-->
<label >Select City</label>
<select id='city_id' name='city' >
<option>-city-</option>
</select>
0 comments:
Post a Comment