在Codeigniter中填充动态下拉列表

时间:2016-06-16 11:25:31

标签: php jquery codeigniter model-view-controller

数据库包括艺术,科学和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中做到这一点,得到谷歌的帮助像阿贾克斯骗局但没什么

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

<强>控制器(索引)

<?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;
    }

}

<强>模型(Countries_model.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是显示国家列表的视图。