Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

php - I Want To Build Update Form When I Change Main Dropdown Menu Then update the second dropdown list

                     <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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think u want something like this

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>

controller:

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

model:

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

?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...