0

Database inlcude Groups like Arts,Science and IT.By Choosing one Group Second Dynamic Dropdown appear which will show the Subjects Related to Group. This is my View

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

And This is Controller

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

I didnt know how to do this in Codeigniter,Got helped from google like Ajax trickds but nothing

Saad Mirza
  • 1,154
  • 14
  • 22
  • `` can be `load->view('admin_header'); ?>` in CI.... :) – Praveen Kumar Jun 16 '16 at 11:30
  • 1
    onChange event should trigger a javascript function. `onchange="/courses/getsubject'+this.value"` need to refer to JS function. Also your controller method doesn´t return anything. I would use jQuery to load the data, ajax calls is easier that way, I think. – jtheman Jun 16 '16 at 11:41
  • but how to get this value in the controller ,in the parameters? and then how next dropdown will be updated? – Saad Mirza Jun 16 '16 at 11:51
  • can you provide the code? – Saad Mirza Jun 16 '16 at 11:51
  • What does that even mean?? "Got helped from google like Ajax trickds but nothing" – Jonathan Jul 31 '21 at 12:46
  • There would need to be a slash before your `this.value` value is appended to your url path. `onchange="/courses/getsubject/' + this.value`. Also, `cotainer-fluid` is not the same as `container-fluid` – mickmackusa Aug 01 '21 at 02:11
  • I recommend [not performing any ajax, but loading all parent-child relationships as data passed from the controller to the view](https://stackoverflow.com/a/34128745/2943403). – mickmackusa Apr 28 '23 at 01:55

2 Answers2

0

Here is an example:

Controller(Index)

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

}

Model(Countries_model.php)

<?php

class Countries_model extends CI_Model {

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

}

View(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>

View (cities.php)

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

To do this I used jquery, so it's easier. It's necessary two views, index.php is the main view and cities.php is the view to show the list of countries.

Alexander Ceballos
  • 750
  • 2
  • 20
  • 36
  • Source: http://www.lax-soft.tk/index.php/programacion/php-y-jquery/10-combobox-dependiente-con-codeigniter-y-jquery – Alexander Ceballos Jun 16 '16 at 20:01
  • 1
    Demo: http://www.lax-soft.tk/codeIgnitercbx/, check all countries, some countries don´t have associated cities, or maybe check the configuration of your server – Alexander Ceballos Jun 19 '16 at 21:25
  • `query()` shouldn't ever be called from the Controller layer -- this is what the Model is for. – mickmackusa Jul 31 '21 at 23:27
  • Also, you should be using placeholders when you pass user input into a sql query for security/stability. Ulimately, ajax calls to the server may be overkill for relatively small volumes of data. In some cases, it is more ideal to create a multidimensional lookup array, pass it to the view, where is is converted to a json object and referenced on `onchange` events. If using ajax, it would be a good idea to cache the response as a json object so that if the user ever re-selects a specific option, then the server is not bothered by a redundant call. – mickmackusa Jul 31 '21 at 23:42
0

just send array and get on view page.

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