Uthpala Pitawela
Uthpala Pitawela

Reputation: 113

I could not get the returned value from the controller to the ajax success function inthe view in CI

This is the ajax function in the view

 <script type="text/javascript">

        $('#name').on('change',function(){
            var uid = document.getElementById('name').value;

            $.ajax({
                    url : '<?php echo base_url('index.php/Manager_Settings_Controller/getUsername');?>',
                    method : 'get',
                    data : {'uid' : uid },

                    success : function(result){

                            console.log(result);


                            }
        });

        });

This is the getUsername function from the controller

 public function getUsername(){
    $uid = $this->input->get('uid');
    $this->load->model('Manager_Settings_Model');
    $data = $this->Manager_Settings_Model->getUsername($uid);
    return $data['username'];


}

And this is the model function

    function getUsername($uid){
         $this->db->select('username');
         $query = $this->db->get_where('user',array('u_id'=>$uid));
         foreach($query -> result() as $row){
             $data = array(
                'username' => $row->username,


                );
            }
        return $data;

}

Its really great if someone can help me. Thanks inadvance

Upvotes: 0

Views: 101

Answers (1)

user5189304
user5189304

Reputation:

  public function getUsername(){
        $uid = $this->input->get('uid');
        $this->load->model('Manager_Settings_Model');
        $data = $this->Manager_Settings_Model->getUsername($uid);
        echo $data['username'];      
    }

You got to echo not return in controller, so you get the error or result whatever in console.log in success function of ajax.

Upvotes: 1

Related Questions