繁体   English   中英

从基于代码选择器中的第一个选择框选择从数据库中获取第二个选择的值

[英]getting values of second select from db based of first select box selection in codeigniter

我需要有关如何基于第一个选择框获取第二个选择框的值的帮助

这是视图:

$(document).ready(function() {

$('#state').change(function() {
// Get an instance of the select, and it's value.
    var state = $(this),
    stateID = state.val();
// Add if statement to check if the new state id
// is different to the last to prevent loading the same
// data again.

// Do the Ajax request.
$.ajax({
    url : 'http://localhost/ci_ajax/select/get_cities/'+stateID, // Where to.
    dataType : 'json', // Return type.
    success : function(data) { // Success :)
        // Ensure we have data first.
        if(data && data.Cities) {
            // Remove all existing options first.
            state.find('option').remove();
            // Loop through each city in the returned array.
            for(var i = 0; i <= data.Cities.length; i++) {
                // Add the city option.
                state.append($('option').attr({
                    value : data.Cities[i].value
                }).text(data.Cities[i].city));
            }
        }
    },
    error : function() {
        // Do something when an error happens?
    }
});

}); });

<form action="" method="post">
<select name="state" id="state">
    <option>Select</option>
    <?php foreach($states as $row):?>
        <option value="<?php echo $row->id?>"><?php echo $row->states;?></option>
    <?php endforeach;?>
</select>
<select id="cities" name="cities">
    <option>Select</option>
</select>

这是控制器:

类Select扩展CI_Controller {

function index(){

    $data['states']=$this->load_state();
    $this->load->view('form',$data);
}

function load_state(){

    $this->load->model('data_model');

    $data['states']=$this->data_model->getall_states();

    return $data['states'];
}

function get_cities() {
     // Load your model.
    $this->load->model('data_model');
    // Get the data.
    $cities = $this->data_model->get_cities();
    // Specify that we're returning JSON.
    header('content-type: application/json');
    // Return a JSON string with the cities in.
    return json_encode(array('Cities' => $cities));
}

}

这是模型:

Data_model类扩展了CI_Model {

function getall_states(){

    $query=$this->db->get('states');
    if($query->num_rows()>0){
        foreach($query->result() as $row){
            $data[]=$row;
        }
        return $data;
    }

}

function get_cities(){

    $this->db->select('id,cities');
    $this->db->from('cities');
    $this->db->where('s_id',$this->uri->segment(3));
    $query=$this->db->get();

    if($query->num_rows()>0){
        foreach($query->result() as $row){
            $data[]=$row;
        }
        return $data;
    }

}

}

请对此提供帮助,以提供正确的代码。

因为您是直接访问get_cities()函数,而不是从控制器中的另一个函数访问,所以return语句实际上不会将json数组打印到页面上。

return json_encode(array('Cities' => $cities));

有3种打印方法:第一种是printecho显json数组(不好的做法),第二种是使用视图打印发送给它的原始文本。 IE浏览器

$this->load->view('raw', array('data' => json_encode(array('Cities' => $cities)));

与raw.php只是:

<?php print $data; ?>

或者最后,您可以在output类中使用set_output()函数,如下所示:

$this->output->set_output(array('data' => json_encode(array('Cities' => $cities)));

如果只想从index()函数访问它,您可能还想使它的function load_state()私有。

您的代码可能还有其他问题,但这是最明显的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM