dp0518
dp0518
2016-01-06 09:40

如何在Codeigniter中的View中将特定行传递给ajax函数

已采纳

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..');
    }
   });
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dtkmejg127475 dtkmejg127475 5年前

    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);
        }
    
    }
    
    点赞 评论 复制链接分享
  • dongxinxin7809 dongxinxin7809 5年前

    model should be like this

    public function get_last_course_rec($last_id)
    {
       return $this->db->select('course')->from('table_name')->where('id',$last_id)->get()->row();
    
    }
    

    controller

    $result_updated_record = $this->course_model->get_last_course_rec($id);
    header('Content-Type: application/json');
    if($result_updated_record)
        { 
           echo json_encode($result_updated_record);
    
        }
        else
        {   
            echo json_encode("no result");
        }
    
    点赞 评论 复制链接分享

相关推荐