如何在代码点火器中创建依赖下拉列表


How to make a dependent dropdown in codeigniter

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

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

身份证state_name1 英里2 向上

身份证state_index city_name1 1 博帕尔2 1 印多尔3 2 巴特那

我的控制器在这里(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>

尝试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));

最简单的方法是在 Codeigniter 中创建一个依赖下拉列表。

控制器(学校.php)

<?php
public function get_rollno()
{
    if(!empty($_POST["id"]))
    {
    $id=intval($_POST['id']);
    $data['users']=$this->School_model->select_data('users','*',array('type'=>'2','classid'=>$id));
    $this->load->view('get_rollno',$data);
?>

型号 (School_model.php)

<?php
public function select_data($tbl_name,$field,$warr='')
{
    if($warr!='')
    {
        $this->db->where($warr);
    }
    
    $res=$this->db->select($field)->from($tbl_name)->get();
    return $res->result_array();
}
?>

视图(索引.php)

<form method="post" id="import_form" enctype="multipart/form-data">
   <label>Select Class</label>
   <select name="classid" id="classid" class="span6" onChange="getRollNo(this.value);" required>
   <option value="">--Class--</option>
   <?php foreach($class as $cls){?>
   <option value="<?php echo $cls['id'];?>"><?php echo $cls['class_name'];?> 
   </option>
   <?php }?>
   </select>
   <label>Select Roll No.</p></label>
   <select name="roll_no" id="roll_no" class="span6"></select>
   <input type="submit" value="Import" name="import" class="btn btn-primary">
 </form>

Jquery (Custom.js)

function getRollNo(val) {
$.ajax({
type: "POST",
url: "http://localhost/codeigniter/index.php/school/get_rollno",
data:'id='+val,
success: function(data){
$("#roll_no").html(data);
}
});
}

视图 (从此文件获取数据) (get_rollno.php)

<option value="">Rool Numbers</option>
<?php foreach($users as $usr){?>
<option value="<?php echo $usr['id'];?>"><?php echo $usr['roll_no'];?> 
</option>
<?php }?>