AJAX不使用codeigniter

时间:2016-05-17 06:17:46

标签: php 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>

当我尝试使用它运行的示例代码时,但是当我尝试使用我的表更改变量时它不起作用,请告诉我哪些行出错?

2 个答案:

答案 0 :(得分:1)

尝试传递此数据的数据:{&#39; country&#39;: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>

答案 1 :(得分:0)

检查控制器功能时,应使用die或exit函数停止请求,

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