jquery - populate a dropdown depending on first dropdown -
i want populate whoom-to-meet dropdown box after select department department-to-go dropdown box,but when click on department list departments show when choose department related employees department not showing in whoom-to-meet dropdown list. below 2 table. department table
+----+-----------+ | id | dept_name | +----+-----------+ | 2 | hr | +----+---------- +
user_master table
+----+-------------+-----------+----------+ | id | emp_name | dept_id | type | +----+-------------+-----------+----------+ | 1 | dipti | 2 | employee | + ---+-------------+-----------+----------+
my controller
public function create_pass() { $pageid = 1; $data['hh'] = $this->admin_model->get_dept(); $this->load->view('header'); $this->load->view('sidebar'); $this->load->view('createpass', $data); $this->load->view('footer'); } public function select_item_by_dept() { if ($this->session->userdata('admin_logged_in')) { $category_id = $this->input->post('category_id'); $data['result'] = $this->admin_model->select_item_by_cat($category_id); $html = ""; foreach ($data['result'] $list) { $html.="<option value='" . $list['dept_id'] . "'>" . $list['emp_name'] . "</option>"; } echo $html; } else { $this->load->view('login'); } }
my model
function select_item_by_cat($category_id) { $this->db->where('dept_id', '$category_id'); $query = $this->db->get('user_master'); return $query->result_array(); } function get_dept() { $this->db->select("*"); $this->db->from('department'); $query = $this->db->get(); return $query->result_array(); }
my view
<div class="form-group"> <label for="inputpassword3" class="col-sm-3 control-label">department go </label> <div class="col-sm-3"> <select class="form-control select2" name="dptgo" id="dptogo" onchange="change_category(this.options[this.selectedindex].value)" style="width: 100%;"> <option selected="selected">select</option> <?php foreach($hh $datas) { ?> <option value="<?php echo $datas['id']; ?>"><?php echo $datas['dept_name']; ?></option> <?php } ?> </select> <div class="text-danger"><?php echo form_error('dptgo'); ?></div> </div> <label for="inputpassword3" class="col-sm-2 control-label">whom meet</label> <div class="col-sm-4"> <select class="form-control select2" id="whomtmt" name="wtomeet" style="width: 100%;"> <option selected="selected">select</option> <option></option> </select> </div> </div>
my jquery
<script> function change_category(category_id) { if (category_id == "select") { $("#whomtmt").html("<option>select</option>"); $("#whomtmt").trigger("chosen:updated"); $("#whomtmt").trigger("liszt:updated"); } else { loaddata(category_id); } } function loaddata(category_id) { var datastring = 'category_id=' + category_id; $.ajax({ type: "post", url: "<?php echo site_url(); ?>/admin/select_item_by_dept", data: datastring, cache: false, success: function (result) { $("#whomtmt").html("<option>select</option>"); $("#whomtmt").append(result); $("#whomtmt").trigger("chosen:updated"); $("#whomtmt").trigger("liszt:updated"); } }); } </script>
in model, in select_item_by_cat function
this wrong $this->db->where('dept_id', '$category_id');
the query executes
select * `user_master` `dept_id` = '$category_id'
so change
$this->db->where('dept_id', $category_id);
and try can helps :)
Comments
Post a Comment