Codeigniter Ajax Country State City Drop Down

Sample database design for table name country.
This table contains  id (primary key) and country_name.
CREATE TABLE IF NOT EXISTS `country` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `country_name` varchar(500) NOT NULL
)
Sample database design for table name state.
This table contains  id (primary key), country_id and state_name.
CREATE TABLE IF NOT EXISTS `state` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `country_id` int(11) NOT NULL,
  `state_name` varchar(500) NOT NULL
)
Sample database design for table name city.
This table contains  id (primary key), state_id and city_name.
CREATE TABLE IF NOT EXISTS `city` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `state_id` int(11) NOT NULL,
  `city_name` varchar(500) NOT NULL
)
 
Java Script Code:-
 
function selectState(country_id){
  if(country_id!="-1"){
    loadData('state',country_id);
    $("#city_dropdown").html
("<option value='-1'>Select city</option>");
  }else{
    $("#state_dropdown").html
("<option value='-1'>Select state</option>");
    $("#city_dropdown").html
("<option value='-1'>Select city</option>");
  }
}
 
function selectCity(state_id){
  if(state_id!="-1"){
   loadData('city',state_id);
  }else{
   $("#city_dropdown").html
("<option value='-1'>Select city</option>");
  }
}
 
function loadData(loadType,loadId){
  var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
  $("#"+loadType+"_loader").show();
  $("#"+loadType+"_loader").fadeIn(400).html
('Please wait... <img src="image/loading.gif" />');
  $.ajax({
    type: "POST",
    url: "loadData",
    data: dataString,
    cache: false,
    success: function(result){
      $("#"+loadType+"_loader").hide();
      $("#"+loadType+"_dropdown").html
("<option value='-1'>Select "+loadType+"</option>");
      $("#"+loadType+"_dropdown").append(result);
    }
 });
}
 
Controller Functions:-
 
public function index()
{
   $this->load->model('model');
   $result['list']=$this->model->getCountry();
   $this->load->view('top');
   $this->load->view('index',$result);
   $this->load->view('footer');
}

 public function loadData()
 {
   $loadType=$_POST['loadType'];
   $loadId=$_POST['loadId'];
   $this->load->model('model');
   $result=$this->model->getData($loadType,$loadId);
   $HTML="";

   if($result->num_rows() > 0){
     foreach($result->result() as $list){
       $HTML.="<option value='".$list->id."'>".$list->name."</option>";
     }
   }
   echo $HTML;
 }   
 
Model Functions:- 
function getCountry()
{
   $this->db->select('id,country_name');
   $this->db->from('country');
   $this->db->order_by('country_name', 'asc');
   $query=$this->db->get();
   return $query;
}

function getData($loadType,$loadId)
{
   if($loadType=="state"){
    $fieldList='id,state_name as name';
    $table='state';
    $fieldName='country_id';
    $orderByField='state_name';
   }else{
    $fieldList='id,city_name as name';
    $table='city';
    $fieldName='state_id';
    $orderByField='city_name';
   }
   $this->db->select($fieldList);
   $this->db->from($table);
   $this->db->where($fieldName, $loadId);
   $this->db->order_by($orderByField, 'asc');
   $query=$this->db->get();
   return $query;
 }
 
  

No comments: