This post explains you about how to create Codeigniter Ajax Country State City Drop Down.

If you are looking for PHP Ajax Country State City Drop Down, than please follow my previous post

On the basic of selected country, related state will be populated in state dropdown and on the basis of selected state dropdown, related city will be populated in city dropdown.

 

Download Link   Demo Link

 

I am using three datebase table here country,state and city table

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
)
Working Flow:- When page load, at that time controller index function is called and trigger a model function getCountry() and  this function retrieve all the available country name and pass this to view. When country drop down value change it called a ajax function selectState(current country id) and this ajax function called a controller loadData() and this function called a model function loadData(filter type like state or city) and on that basic this model function return data. The same procedure follow for state drown change but with different java script function selectCity(state id) function and follow previous flow. Both the java script function call loadData js function for loading the data.
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;
 }