2016-05-17 46 views
0

我在我的控制器中這樣的代碼,這段代碼我從示例代碼中獲得,但是當我嘗試使用我的表時,它不工作來填充另一個下拉列表。AJAX不與codeigniter

我的控制器

class Bkp extends CI_Controller { 

function __construct() { 
    parent::__construct(); 
    $this->load->model('modelRegister'); 
} 

function carselection() { 

    $arrCarbrand = $this->modelRegister->loadcarbrand(); 

    foreach ($arrCarbrand as $carbrand) { 
     $arrcar[$carbrand->make] = $carbrand->make; 
    } 

    $data['make'] = $arrcar; 

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

function ajax_call() { 

    if (isset($_POST) && isset($_POST['make'])) { 

     $make = $_POST['make']; 
     $arrModels = $this->modelRegister->loadmodelfrombrand($make); 

     //print_r($arrModels); 
     foreach ($arrModels as $models) { 
      $arrmodels[$models->model] = $models->model; 
     } 

     print form_dropdown('model',$arrmodels); 
    } else { 
     redirect('site'); 
    } 
} 
} 

我查看

 <?php 
    $this->load->helper('html'); 
    ?> 
<html> 
    <head> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $('#makecombox select').change(function() { 
        var selMake = $(this).val(); 
        console.log(selMake); 
        $.ajax({ 
         url: "bkp/ajax_call", 
         async: false, 
         type: "POST", 
         data: "make="+selMake, 
         dataType: "html", 

         success: function(data) { 

          $('#model').html(data); 

         }, 
        }) 
       }); 
      }); 
     </script> 
    </head> 

    <body> 
     <div id="mydoubts"> 
      <div id="makecombox"><?php echo form_dropdown('make',$make); ?></div> 
      <div id="model"></div> 
     </div> 
    </body> 
</html> 

當我嘗試使用它的工作示例代碼,但是當我試圖用我的表改變變量它不工作,請告訴我哪一行錯了?

+0

試圖通過這樣的數據的數據:{「國家」:selCountry}, – JYoThI

+0

,你也不必POST名稱,如$ _ POST [「讓」];這是在你的控制器中使用 – JYoThI

+0

您好@jothi我的壞,我粘貼錯誤的觀點,請再次看到上面... –

回答

1

試圖通過這樣的數據的數據:{「國家」:selCountry},

缺少Ajax調用分號

<script type="text/javascript"> 
     $(document).ready(function() { 
      $('#makecombox select').change(function() { 
       var selMake = $(this).val(); 
       console.log(selMake); 
       $.ajax({ 
        url: "bkp/ajax_call", 
        async: false, 
        type: "POST", 
        data: {'make':selMake}, 
        dataType: "html", 

        success: function(data) { 

         $('#model').html(data); 

        } //remove comma here 
       }); //add semicolon here 
      }); 
     }); 
    </script> 
+0

嗨@jothi,我一直在改變觀點,我的壞,我已經給了一個錯誤查看 –

+0

無論如何,我試圖使用這一個,數據:{'make':selMake},但它沒有顯示其他下拉 –

+1

檢查元素和查看網絡選項卡 – JYoThI

0

你檢查你的控制器功能的請求應通過使用模具停止或退出功能,

function ajax_call() { 

    if (isset($_POST) && isset($_POST['make'])) { 

     $make = $_POST['make']; 
     $arrModels = $this->modelRegister->loadmodelfrombrand($make); 

     //print_r($arrModels); 
     foreach ($arrModels as $models) { 
      $arrmodels[$models->model] = $models->model; 
     } 

     print form_dropdown('model',$arrmodels); 
     die; 
    } else { 
     redirect('site'); 
    } 

}

AJAX的重追求數據,

$.ajax({ 
       url: "countrystate_disp/ajax_call", 
       async: false, 
       type: "POST", 
       data: {'make':selMake}, //data should be like this 
       dataType: "html", 
       success: function(data) { 
        $('#state').html(data); 
       } 
      });