我想在更改主下拉菜单时构建更新表单然后更新第二个下拉列表

时间:2015-06-18 09:22:55

标签: php html ajax html5 codeigniter

                     <div>
                        <label for="form-field-select-3">State : </label>
                        <br />
                        <select class="chosen-select form-control" id="State" name="State" data-placeholder="Choose a State...">
                            <option value="0"> </option>
                            <?php
                             $query=$this->db->query("select Address_State from ref_address_state  where Address_State_ID='".$property->State_ID."'");
                             $query=$query->row();   
                             foreach($Address_State as $rows)
                             {
                                if($rows['Address_State']==$query->Address_State)
                                   {

                                            echo "<option value='".$rows['Address_State_ID']."' selected>".$rows['Address_State']."</option>";                                                                    
                                  }
                            else
                                  {
                                           echo "<option value='".$rows['Address_State_ID']."'>".$rows['Address_State']."</option>";                                                                    

                                  }
                            }       
                             ?>
                        </select>
                    </div>
                     <div>
                        <label for="form-field-select-3">City : </label>
                        <br />
                        <select class="chosen-select form-control" id="City" name="City" data-placeholder="Choose a City...">
                            <option value="0">  </option>
                           <?php
                           $query=$this->db->query("select city_name from ref_city  where city_id='".$property->City."'");
                             $query=$query->row();   
                             foreach($city_name as $rows)
                             {
                                if($rows['city_name']==$query->city_name)
                                   {

                                     echo "<option value='".$rows['city_id']."' selected>".$rows['city_name']."</option>";                                                                    
                                    }
                                    else
                                    {
                                    echo "<option value='".$rows['city_id']."'>".$rows['city_name']."</option>";                                                                    

                                    }
                             }
                                  ?>

                        </select>
                    </div>
                    <div>
                        <label for="form-field-select-3">Locality : </label>
                        <br />
                        <select class="chosen-select form-control" id="Locality" name="Locality" data-placeholder="Choose a Location...">
                            <option value="0">  </option>
                               <?php
                             $query=$this->db->query("select locality_name from ref_locality  where locality_id='".$property->Locality."'");
                             $query=$query->row();   
                             foreach($locality_name as $rows)
                             {
                                if($rows['locality_name']==$query->locality_name)
                                   {

                                     echo "<option value='".$rows['locality_id']."' selected>".$rows['locality_name']."</option>";                                                                    
                                    }
                                    else
                                    {
                                    echo "<option value='".$rows['locality_id']."'>".$rows['locality_name']."</option>";                                                                    

                                    }
                             }
                                  ?>
                        </select>
                    </div>
                    </div>
                </div>
            </div>
    </div>

1 个答案:

答案 0 :(得分:1)

我想你想要这样的东西

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

的javascript:

 <script>
    function selectState(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>");
      }
    }

    function selectCity(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>");
      }
    }


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

    </script>

控制器:

<?php

    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;
     }
    ?>

模型:

<?php
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;
 }

?>