如何在codeigniter中创建依赖下拉列表

时间:2014-12-26 07:23:39

标签: javascript php jquery codeigniter

我想应用州和城市依赖下拉。我的问题是,当我选择州时,城市下拉列表没有显示任何值。

我的数据库在这里(州表)

id state_name 1 mp 2上

id state_index city_name 1 1 bhopal 2 1 indore 3 2 patna

我的控制器在这里(main_controller)

    <?php
class Main_controller extends CI_Controller {

function index()
{
    $this->load->model('model_form');
    $this->data['states'] = $this->model_form->get_state();
    $this->load->view('view_form_all',$this->data);

}

function get_cities($state){

        $this->load->model('Model_form','', TRUE);    
        header('Content-Type: application/x-json; charset=utf-8');
        echo(json_encode($this->Model_form->get_cities_by_state($state)));
    } 
}

我的模型是(main_form.php)

 <?php
class Model_form extends CI_Model
{
      function __construct()
    {
            // Call the Model constructor
            parent::__construct();
    }

    function get_state(){
        $query = $this->db->query('SELECT id, state_name FROM state');
        return $query->result();
    }


    function get_cities_by_state ($state, $tree = null){
        $this->db->select('id, city_name');

        if($tree != NULL){
            $this->db->where('state_index', $state);
        }

        $query = $this->db->get('cities');
        $cities = array();

        if($query->result()){
            foreach ($query->result() as $city) {
                $cities[$city->id] = $city->city_name;
            }
            return $cities;
        } else {
            return FALSE;
        }
    } 


} 

我的观点是(view_from_all.php)

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

$('#f_city, #f_city_label').hide();

$('#f_state').change(function(){
    alert('f_state');
    var state_id = $('#f_state').val(); 
        if (state_id != ""){
        var post_url = "<?php echo base_url();?>index.php/main_controller/get_cities/" + state_id;
        $.ajax({
            type: "POST",
             url: post_url,
             success: function(cities) //we're calling the response json array 'cities'
              {
                $('#f_city').empty();
                $('#f_city, #f_city_label').show();
                   $.each(cities,function(id,city) 
                   {
                    var opt = $('<option />'); // here we're creating a new select option for each group
                      opt.val(id);
                      opt.text(city);
                      $('#f_city').append(opt); 
                });
               } //end success
         }); //end AJAX
    } else {
        $('#f_city').empty();
        $('#f_city, #f_city_label').hide();
    }//end if
}); //end change 
</script>
</head>

<body>
<?php echo form_open('main_controller/add_all'); ?>
        <label for="f_state">State<span class="red">*</span></label>
        <select id="f_state" name="f_state">
            <option value="">select any state</option>
            <?php
            foreach($states as $state){
                echo '<option value="' . $state->id . '">' . $state->state_name . '</option>';
            }
            ?>
        </select>
        <label for="f_city">City<span class="red">*</span></label>
        <!--this will be filled based on the tree selection above-->
        <select id="f_city" name="f_city" id="f_city_label"> 
            <option value=""></option>
        </select>
        <label for="f_membername">Member Name<span class="red">*</span></label>
        <!--<input type="text" name="f_membername"/>-->
<?php echo form_close(); ?> 

</body>

</html>

1 个答案:

答案 0 :(得分:1)

尝试使用GET而不是POST,因为如果您已激活CSRF保护,则在发送请求时会出现问题,说您的令牌丢失了,所以只需更改:

$.ajax({
   type: "POST",

到此:

$.ajax({
   type: "GET",

为了娱乐而做的其他改变:

header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->Model_form->get_cities_by_state($state)));

到:

 $result = $this->Model_form->get_cities_by_state($state);
 return $this->output->set_content_type('application/json')
                    ->set_output(json_encode($result));