如何在Codeigniter中进行依赖下拉 [英] How to make a dependent dropdown in codeigniter

查看:110
本文介绍了如何在Codeigniter中进行依赖下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想应用州和城市依赖的下拉菜单.我的问题是,当我选择州时,城市下拉列表不会显示任何值.

我的数据库在这里(状态表)

my database is here (state table)

id state_name 1 mp 2上

id state_name 1 mp 2 up

id state_index city_name 1 1博帕尔 2 1印多尔 3 2 patna

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

我的控制器在这里(main_controller)

my controller is here( 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)

my model is (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)

my view is(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>

推荐答案

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

try GET instead of POST, because if you have CSRF protection activated you will have a problem while sending your request, saying that your token is missing, so just change this :

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

这篇关于如何在Codeigniter中进行依赖下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆