2016-05-17 45 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); 
       } 
      });