Welcome Guest, Not a member yet? Register   Sign In
Dependent drop down not displaying anything
#1

I have this simple dropdown menu in my codeigniter form. When clicking dzongkhag(district) it should display the list of geogs(towns), but, instead it do not do so. Only the list of geog(town) is populated from the database table. The drop down menu that I have is not the best one but that is what I could come up with after trying (for many weeks) many times unsuccessfully. I will be grateful if someone could help me out/solve/advice on this.

Here are the snippets
view.php
Code:
<label >Dzongkhag: </label>
<select id="user_dzongkhag" name="dzongkhag" >
  <option>Select Dzongkhag</option>
  <option value="Bumthang">Bumtang</option>
  <option value="Chhukha">Chhukha</option>
  <option value="Dagana">Dagana</option>
  <option value="Gasa">Gasa</option>
  <option value="Haa">Haa</option>
  <option value="Lhuentse">Lhuntse</option>
  <option value="Monggar">Monggar</option>
  <option value="Paro">Paro</option>
  <option value="Pema Gatshel">Pema Gatshel</option>
  <option value="Punakha">Punakha</option>
  <option value="Samdrup Jongkhar">Samdrup Jongkhar</option>
  <option value="Samtse">Samtse</option>
  <option value="Sarpang">Sarpang</option>
  <option value="Thimphu">Thimphu</option>
  <option value="Trashigang">Trashigang</option>
  <option value="Trashi Yangtse">Trashi Yangtse</option>
  <option value="Trongsa">Trongsa</option>
  <option value="Tsirang">Tsirang</option>
  <option value="Wangdue Phodrang">Wangdue Phodrang</option>
  <option value="Zhemgang">Zhemgang</option>                
</select>
<label >Geog:</label>
<select id="user_geog" name="geog" >
  <option> select geog</option>
</select>
Java script code, I have written it inside the view.php file
Code:
$(document).ready(function(){
  $('#user_dzongkhag').change(function(){
    $('#user_geog').html('<option> Fetching...</option>');

    var user_dzongkhag =$('#user_dzongkhag').val();

    //alert(new_div);

    $.ajax({
      type:"post",

      data:"user_dzongkhag="+user_dzongkhag,
      url:"<?php echo site_url('ajax/select_auth_geog/'); ?>",
      cache:false,
      success:function(resp){
        //console.log(resp);
        //alert(resp);
        $('#user_geog').html(resp);
      }
    });
  });
controller.php
Code:
public function select_auth_geog(){
  $user_dzongkhag= $this->input->post('user_dzongkhag');
  $returnGeog=$this->ajax_model->select_auth_geog($user_dzongkhag);
  foreach ($returnGeog as $key) {
    echo '<option value="'.$key['name'].'">'.$key['name'].' </option>';
  }
}
});
model.php
Code:
public function select_auth_geog($user_dzongkhag){
  if($user_dzongkhag=='Bumtang'){
    $user_dzongkhag=1;
  }elseif($user_dzongkhag=='Chhukha'){
    $user_dzongkhag=2;
  }elseif($user_dzongkhag=='Dagana'){
    $user_dzongkhag=3;
  }elseif($user_dzongkhag=='Gasa'){
    $user_dzongkhag=4;
  }elseif($user_dzongkhag=='Haa'){
    $user_dzongkhag=5;
  }elseif($user_dzongkhag=='Lhuntse'){
    $user_dzongkhag=6;
  }elseif($user_dzongkhag=='Monggar'){
  ......
  ........
Is there anything I need to change?
Reply
#2

Hey karmad

Be good if you could post up the whole function in the model. Also are there any errors coming back on the request or is it empty? You can check using developer tools on your browser eg Chrome via the network tab.
Reply
#3

Instead of
PHP Code:
data:"user_dzongkhag="+user_dzongkhag
use:
PHP Code:
data: { "user_dzongkhag" user_dzongkhag }, 

You can also save yourself a lot of time and effort if you put the district names in an array (in your controller). You can pass this array to your view to build the list of districts. And you can pass it to your model to determine which cities belong to the selected district.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB