如何将特定行传递给 Codeigniter View 中的 ajax 函数
How to pass a particular row to ajax function in View in Codeigniter
我是 codeigniter 的新手,我想将通过模型从控制器返回的行传递给视图中的 ajax 函数。
以下代码显示了我如何从模型获取数据到控制器但无法将数据传递给 ajax 函数
view.I 在 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..');
}
});
只需回显您的结果,
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);
}
}
模型应该是这样的
public function get_last_course_rec($last_id)
{
return $this->db->select('course')->from('table_name')->where('id',$last_id)->get()->row();
}
控制器
$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");
}
我是 codeigniter 的新手,我想将通过模型从控制器返回的行传递给视图中的 ajax 函数。 以下代码显示了我如何从模型获取数据到控制器但无法将数据传递给 ajax 函数 view.I 在 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..');
}
});
只需回显您的结果,
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);
}
}
模型应该是这样的
public function get_last_course_rec($last_id)
{
return $this->db->select('course')->from('table_name')->where('id',$last_id)->get()->row();
}
控制器
$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");
}