Search code examples
phpajaxcodeignitercodeigniter-2

How to pass a particular row to ajax function in View in Codeigniter


I am new to codeigniter and i want to pass row returned from controller via model to ajax function in a view. The following code shows how i am getting data from model to controller but cannot pass data to ajax function in a view.I am getting null value in success section of ajax.

Model:

public function get_last_course_rec($last_id)
{
   $this->db->where('id',$last_id);
   $update=$this->db->get('course');
   $get_row=$update->row();
}

Controller:

public function create_course_goal(){


    $id=$this->input->post('c_id');  

    $result_updated_record = $this->course_model->get_last_course_rec($id);

    if($result_updated_record!='false')
    { 
        $this->output->set_output(json_encode($result_updated_record));

    }
    else
    {   
        $this->output->set_output(json_encode($result_updated_record));
    }

}

View:

$.ajax({
    type: 'POST',
        url: "<?php echo base_url();?>create/create_goal",
    cache: false,               
    data: dataString,
        dataType:'JSON',
    success: function(data){
        alert("data"+data);
        },
    error: function(){                      
    alert('Error while request..');
    }
   });

Solution

  • Simply Echo Your Result,

    public function create_goal(){
    
    
        $id=$this->input->post('c_id');  
    
        $result_updated_record = $this->course_model->get_last_course_rec($id);
    
        if($result_updated_record!='false')
        { 
            echo json_encode($result_updated_record);
        }
        else
        {   
            echo json_encode($result_updated_record);
        }
    
    }