Skip to content Skip to sidebar Skip to footer

I Want To Build Update Form When I Change Main Dropdown Menu Then Update The Second Dropdown List

Solution 1:

I think u want something like this

http://www.91weblessons.com/codeigniter-ajax-country-state-city-drop-down/

javascript:

<script>functionselectState(country_id)//on change of country select box
    {
      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>");
      }
    }

    functionselectCity(state_id)//on change of state select box
    {
      if(state_id!="-1"){
       loadData('city',state_id);
      }else{
       $("#city_dropdown").html("<option value='-1'>Select city</option>");
      }
    }


    functionloadData(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);
        }
     });
    }

    </script>

controller:

<?phppublicfunctionindex()
    {
       $this->load->model('model');
       $result['list']=$this->model->getCountry();
       $this->load->view('top');
       $this->load->view('index',$result);
       $this->load->view('footer');
    }

     publicfunctionloadData()
     {
       $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:

<?phpfunctiongetCountry()
{
   $this->db->select('id,country_name');
   $this->db->from('country');
   $this->db->order_by('country_name', 'asc');
   $query=$this->db->get();
   return$query;
}

functiongetData($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;
 }

?>

Post a Comment for "I Want To Build Update Form When I Change Main Dropdown Menu Then Update The Second Dropdown List"