2017-04-13 9 views
0

州と都市のドロップダウンを含む登録フォームがあります。しかし、州を選択した後、都市のリストを取得することはできません。コンソールでドロップダウンAjaxの州と都市は機能していません

次は答えのように私に表示されます:

は、リソースの読み込みに失敗しました:サーバーは、404の状態(見つかりません)と答えました。領域のy comuna

|----------------------------| 
    |  tbl_state   | 
    |----------------------------| 
    |  id_state   | 
    |  name_state   | 
    |----------------------------| 

    |----------------------------| 
    |  tbl_city   | 
    |----------------------------| 
    |  id_city    | 
    |  name_city   | 
    |  state_id   | 
    |----------------------------| 

タブラスは、ここに私のコードです。

コントローラー:

public function index() 
    { 
     //State 
     $data['result_state'] = $this->state_model->getState(); 

     $this->load->view('interprete_registro_view',$data); 

    } 

    public function city() 
    { 
     $id = $this->input->get('id_state'); 

     $this->state_model->getCity($id); 
    } 

モデル:

public function getState() 
    { 
     $this->db->select('*'); 
     $this->db->from('tbl_state'); 
     $query = $this->db->get(); 
     $query_result = $query->result(); 
     return $query_result; 
    } 

    public function getCity($id) 
    { 
     $query = $this->db->where('state_id', $id)->get('tbl_city'); 

     $cad = ""; 

     foreach ($query->result_array() as $row) { 
      $cad .= "<option value='{$row['id_city']}'>{$row['name_city']}</option>"; 
     } 

     echo $cad; 
    } 

ビュー:

<script type="text/javascript"> 
    var path = '<?php echo base_url()?>index.php/'; 

    $(document).ready(function() { 
    carga(); 

    $('#state').change(carga); 

    }); 

    function carga() { 
    var cd = $('#state').val(); 

    $.get(path + 'registre/city', {'id' : cd}, function(resp) { 
     $('#city').empty().html(resp); 
    }); 
    } 

</script> 
     <div class="col-2"> 
      <label for="state">State <span> * </span></label> 
     </div> 

     <div class="col-3"> 
      <select class="form-control" name="state" id="state">  
      <option value="" selected>- state -</option> 
       <?php foreach($result_state as $row):?> 
       <option value="<?php echo $row->id_state;?>"><?php echo $row->name_state;?></option> 
       <?php endforeach; ?> 
      </select> 
     </div> 

     <div class="col-2"> 
      <label for="city">City <span> * </span></label> 
     </div> 

     <div class="col-3"> 
      <select class="form-control" name="city" id="city"> 
      <option>- city -</option>" 
      </select> 
     </div> 

任意のアイデア、ソリューションまたはディメンション歓迎です。

+0

「http:// www.yourUrl.com/index.php/registre/city'はブラウザで単独で動作するのですか404を返しますか?それがうまくいけば、あなたのスクリプトの 'path'が正しく設定されていますか? – ourmandave

+0

'config.php'で '$ config ['base_url']'に割り当てられているものは? '.htaccess'を使ってURLを書き換えていますか? – DFriend

答えて

0

ビューでは、JavaScriptは名前がidのgetパラメータを渡していますが、コントローラではid_stateを取得しようとしています。

function carga() { 
    var cd = $('#state').val(); 

    $.get(path + 'registre/city', {'id_state' : cd}, function(resp) { 
     $('#city').empty().html(resp); 
    }); 
    } 
関連する問題