在Codeigniter中填充动态下拉列表

eivnm1vs  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(213)

数据库包括像艺术,科学和IT组。通过选择一个组第二动态下拉菜单出现,将显示与组相关的主题。这是我的看法

<?php include('admin_header.php'); ?>
   <body id="page-wrapper">
 <div class="container-fluid">
    <h1 class="text-center">Enter Courses</h1>
 <hr>
 </div>
 <?php if($feedback = $this->session->flashdata('feedback')):
  $feedback_class = $this->session->flashdata('feedback_class');
  ?>
 <div class="col-lg-6 col-lg-offset-3">
   <div class="alert alert-dismissible <?= $feedback_class ?>">
      <button type="button" class="close" data-dismiss="alert">&times;</button>
      <p><?= $feedback ?></p>
    </div>
 </div>
   <?php endif; ?>
 <div class="cotainer-fluid">
        <div class="col-lg-6 col-lg-offset-3">
         <?php echo form_open('courses/input',['class'=>'form-group']); ?>
            <br>
  <?php echo form_input(['name'=>'rollno','class'=>'form-control','placeholder'=>'Enter Student ROll NO']); ?>
            <br>
            <select name="group" class="form-control"     onchange="/courses/getsubject'+this.value" >
                <option value="Arts">Arts</option>
                <option value="Science">Science</option>
                <option value="IT">IT</option>
            </select>

            <br>
            <?php echo form_dropdown(''); ?>
            <br>
            <?php echo form_submit(['class'=>'form-control btn btn-         success','value'=>'Submit']) ?>
        <?php echo form_close(); ?>
    </div>
 </div>
 </body>

这是控制器

<?php
class Courses extends MY_Controller
  {
public function assign()
{
    $this->load->helper('form');
    $this->load->view('admin/course');
}
public function getsubject($grp)
{
        $this->load->model('courses');
$grp = $this->courses->getsubjects($grp);
  }}
     ?>

我不知道如何在Codeigniter中做到这一点,得到了谷歌的帮助,比如 AJAX 技巧,但什么都没有

xfb7svmp

xfb7svmp1#

下面是一个例子:

控制器(步进)

<?php

class Index extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url_helper');
        $this->load->model('countries_model');
    }

    public function index() {
        $query = $this->db->query('SELECT id,name FROM countries');
        $data['countries'] = $query->result_array();
        $this->load->view('index/index', $data);
    }

    public function cities() {
        $country_id = $this->input->post('country_id');
        $query = $this->db->query('SELECT id,name FROM cities WHERE country_id=' . $country_id);
        $data['cities'] = $query->result_array();
        $this->load->view('index/cities', $data);
        echo $country_id;
    }

}

模型(国家/地区_模型. php)

<?php

class Countries_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

}

查看(index.php)

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dymanic Selects</title>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.js" ></script>

<script type="text/javascript">
$(document).ready(function(){
    $('#sl_countries').change(function(){
            $('#sl_cities').load("<?php echo site_url('index/cities') ?>",{country_id:$(this).val()});
        }); 
});
</script>

</head>
<body>
    <label>Countries</label>
    <select id="sl_countries">
        <?php foreach($countries as $c):?>
        <option value="<?php echo $c['id']?>"><?php echo $c['name']?></option>
        <?php endforeach;?>
    </select>

    <label>Cities</label>
    <select id="sl_cities"></select>
</body>
</html>

查看(cities.php)

<?php foreach ($cities as $c): ?>
    <option value="<?php echo $c['id'] ?>"><?php echo $c['name'] ?></option>
<?php endforeach; ?>

我使用jquery来实现这个功能,这样更简单。需要两个视图,index.php是主视图,cities.php是显示国家列表的视图。

wxclj1h5

wxclj1h52#

只需发送数组并进入查看页面。

<select id="sl_countries">
        <?php foreach($countries as $c):?>
        <option value="<?php echo $c['id']?>"><?php echo $c['name']?></option>
        <?php endforeach;?>
    </select>

相关问题