4

Unable to get value from database in codeigniter. I tried to fetch data based on select box value(menu_code) without refreshing page using ajax but I got result undefined.

This my controller's code : login.php

public function get_menu_rights()
{
    if (isset($_POST['name']))
    {       
        $root_id = $this->input->post('menu_root_id');

        $data['res'] = $this->login_model->get_menu_check($root_id);
        // print_r($data['res']);
        echo json_encode($data);
        //$this->load->view('pages/role_rights',$data);     
    }
}

Below is my model code login_model.php

public function get_menu_check($root_id)
{       
    $this->db->select('menu_code,menu_name');       
    $this->db->from('create_menu as C1');   
    $this->db->where('C1.menu_root_id',$root_id);       
    $this->db->order_by('menu_code');

    return $this->db->get()->result_array();
}

This is my view code role_rights.php

<form action="<?php echo base_url('login/get_menu_rights');?>" method="post">
    <?php
     print"<select class=\"form-control\" name=\"menu_root_id\" onchange=\"javascript:__doPostBack();\" id=\"menu_root_id\">"; ?>                                       <option value="select">select</option>
                        <?php foreach($result as $res) { ?>                             
                    <option value="<?php echo $res->menu_code; ?>">
                    <?php echo $res->menu_name.'-'.$res->menu_code; ?>
                    </option>
                <?php } ?>
            </select>
            </form>
        </div>
    <script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>

<script type="text/javascript">
        function __doPostBack()
        {
            var name = document.getElementById('menu_root_id').value; 
            var dataString='name='+ name;
            $.ajax({
                type:"post",
                url:"<?php echo base_url('login/get_menu_rights'); ?>",
                data:dataString,
                cache:false,
                dataType: 'json',                  
              success: function(data)        
              {
                var id = data[0];             
                var vname = data[1]; 
                $('#output').html("<b>menu_code: </b>"+id+"<b> menu_name: </b>"+vname); 
              }
            });
            return false;
        }
        </script>
</div>

<div id="output"></div>
3
  • $data['res'] = ... You're putting the result row within 'res'. So you need to access it the same way. data.res.menu_code Commented Jul 19, 2018 at 14:13
  • yes i tried but it doesn't work dude. Commented Jul 19, 2018 at 14:20
  • Put the JSON returned by the request in your question Commented Jul 19, 2018 at 14:21

3 Answers 3

1

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); 
}
Sign up to request clarification or add additional context in comments.

11 Comments

but still it displays like this (menu_code: undefined menu_name: undefined). i passing my selected box value into ajax and getting response from ajax.So,i think i maked mistake in ajax..but i donno where...
where and on which file/line u r getting that error?
i got error on <div id="output"></div> i think i'm not properly getting response using ajax from controller
i got all values from db ...response in console successfully bro but how to display in my view file?{res: Array(2)} res : Array(2) 0 : {menu_code: "2", menu_name: "Plant"} 1 : {menu_code: "3", menu_name: "Line"} length : 2 proto : Array(0) proto : Object
ok , parse this data in your ajax, since it contains array data so you have to loop it see my updated answer for success function
|
1

There are a few things I noticed

  1. $data is an undefined array & you are settings the result array returned by the model function to it's 'res' key
  2. dataString is not a json neither it's a js array that you are sending
  3. since you used json_encode, you need to use JSON.parse(data) in the ajax success
  4. 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 get id from data[0]

Comments

0

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

5 Comments

still it displays undefined
please use last_query function.to check the query result like
$this->db->last_query(); add this statement after get(); function and copy the result and direct check in phpmyadmin.if the query give you some result then the query is correct and the error was in your controller.
on ajax call in success just put console.log(data) at the top of function and watch the console. what is return ?
got response in console but how to display in view file? {menu_code: "2", menu_name: "Plant"} {menu_code: "3", menu_name: "Line"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.