how to get values from database in codeigniter based on select box value without refreshing page?
There are a few things I noticed
$data
is an undefined array & you are settings the result array returned by the model function to it's'res'
keydataString
is not a json neither it's a js array that you are sending- since you used
json_encode
, you need to useJSON.parse(data)
in the ajax success - if you do have the result in
$data['res']
, then you need to do something like this -data=JSON.parse(data)['res'];
now you can getid
fromdata[0]
Hope this will help you :
Replace
$root_id = $this->input->post('menu_root_id');
with
$root_id = $this->input->post('name');
Your controller's get_menu_rights
method should be like this :
public function get_menu_rights(){ $root_id = $this->input->post('name'); if(! empty($root_id)) { $data = $this->login_model->get_menu_check($root_id); // print_r($data); echo json_encode($data); exit; }}
Your ajax success
function should be like this :
success: function(data) { var html = ''; $.each(data,function(k,v){ alert(v); html += "<b>menu_code: </b>"+v.menu_code+"<b> menu_name: </b>"+v.menu_name }); $('#output').html(html); }
I think the query return empty please try this Code.....
public function get_menu_check($root_id) { $data = $this->db->select('C1.menu_code,C1.menu_name') ->from('create_menu as C1') ->where('C1.menu_root_id',$root_id) ->order_by('C1.menu_code') ->get(); if($data->num_rows() >= 0) return $data->result_array(); else return false; }