【问题标题】:getting values of second select from db based of first select box selection in codeigniter根据codeigniter中的第一个选择框选择从db中获取第二个选择的值
【发布时间】:2013-06-03 21:08:32
【问题描述】:

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

这是视图:

$(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>

这是控制器:

类选择扩展 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;
    }

}

}

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

【问题讨论】:

  • 您看到了什么错误?我注意到您正在删除/附加选项到“州”选择,而不是“城市”选择,这可能是您的问题吗?
  • 您是否遇到任何有助于帮助的错误...考虑到您的问题有多简单,需要查看很多代码
  • 没有得到任何错误只是空白数组
  • 能否为我提供此类过程的完整源代码
  • 这在什么时候失败了?你能填充第一个选择框吗?您是否能够通过 AJAX 将选定的“状态”发送回您的 PHP 脚本?当您运行查询以获取城市时,您是否得到任何值?

标签: php codeigniter codeigniter-2


【解决方案1】:

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

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

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

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

如果您的function load_state() 只能通过index() 函数访问,您可能还希望将其设为私有。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2014-01-08
    • 2020-04-27
    • 1970-01-01
    相关资源
    最近更新 更多