[英]How to pass parameter from javascript to controller in codeigniter?
I want pass parameter from javascript to codeigniter controller. 我想将参数从javascript传递到codeigniter控制器。 How to do this.
这个怎么做。 My javascript file is like below
我的JavaScript文件如下
function confirmcourse(){
$('.confirm').unbind("click");
$('.confirm').on("click",function(e){
var courseId=$(this).attr('data-id');
$.ajax({
type: "POST",
data:{cid: courseId},
url: urljs+"user/Teach_controller/confirmcourse",
dataType:'json',
}).done(function( data ) {
if(data.result=1){
window.location.assign(urljs+"user/Learn_controller");
}else{
noty({ text: 'Something went wrong during process please try again',type: 'error',modal:true,timeout:1000});
window.location.reload();
}
});
});
}
public function index($course_type ==''){
$id=$this->session->userdata('cp_userid');
$userdetails=$this->Usermodel->getuserdetails($id);
$data['userdetails']=$userdetails;
$result=$this->Usermodel->currentlearningcourses($id);
$data['details']=$result;
$data['content']=$this->load->view("user/currentlearningcourses",$data,true);
if($course_type == 'teaching'){
$result = $this->Usermodel->teachingcourses($id);
$data['details'] = $result;
$data['content'] = $this->load->view("user/teachingcourses",$data,true);
}
$data['title']='My Courses';
$headerContent = $this->load->view("user/layout",$data,true);
$this->render($headerContent);
}
I want to take that teaching parameter to index of Learn_controller from the javascript file. 我想将该教学参数从javascript文件索引到Learn_controller。
You should read it in controller with $_POST['cid']
您应该使用
$_POST['cid']
在控制器中阅读它
But controller response of an ajax call can't be a view is a good practice that it be a JSON
但是ajax调用的控制器响应不能是视图,这是一个
JSON
是一个好习惯
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));
in your case with $data['result'] = 1;
在你的情况下
$data['result'] = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.