Php Ajax函数调用:在codeigniter中传递给控制器函数的参数
我正在研究codeigniter。我正在视图页面中调用ajax函数。AJAX 函数正在调用控制器方法。Ajax函数包含3个 我想传递给控制器方法的参数,但由于某些原因 我无法访问来自ajax函数的参数 以下是我的视图代码:Php Ajax函数调用:在codeigniter中传递给控制器函数的参数,php,codeigniter,parameters,codeigniter-3,Php,Codeigniter,Parameters,Codeigniter 3,我正在研究codeigniter。我正在视图页面中调用ajax函数。AJAX 函数正在调用控制器方法。Ajax函数包含3个 我想传递给控制器方法的参数,但由于某些原因 我无法访问来自ajax函数的参数 以下是我的视图代码: <script> $('#drp').change(function(e){ var costcenter = $('#costcenter_id :selected').val(); var location1 = $('#lo
<script>
$('#drp').change(function(e){
var costcenter = $('#costcenter_id :selected').val();
var location1 = $('#location_id :selected').val();
var department = $('#department_id :selected').val();
$.ajax({
cashe: false,
type: 'POST',
data: {'costcenterid':costcenter,'locationid':location1,
'departmentid':department},
url: 'http://local.desk.in/mycontroller/contollerfunction',
success: function(data)
{
alert("success");
}
});
});
</script>
// controller method
public function controllerfunction($costcenterid,$locationid,$departmentid)
{
echo "costcenter= ". $costcenterid;
echo "location= ". $locationid;
echo "department= ". $departmentid;
}
$('#drp')。更改(函数(e){
var costcenter=$('#costcenter_id:selected').val();
var location1=$('#location_id:selected').val();
var department=$('#department_id:selected').val();
$.ajax({
现金:错,
键入:“POST”,
数据:{'costcenterid':costcenter,'locationid':location1,
“部门ID”:部门},
网址:'http://local.desk.in/mycontroller/contollerfunction',
成功:功能(数据)
{
警惕(“成功”);
}
});
});
//控制器方法
公共功能控制器功能($costcenterid、$locationid、$departmentid)
{
echo“costcenter=”.$costcenterid;
echo“location=”.$locationid;
echo“department=”.$departmentid;
}
获取错误消息:
消息:assetcontroller::controllerfunction()缺少参数1,
消息:assetcontroller::controllerfunction()缺少参数2,
消息:缺少assetcontroller::controllerfunction()的参数3
为什么不能将ajax参数值发送到控制器方法??谢谢你
前进希望这对您有所帮助: 您在ajax中以post的形式传递数据,因此您必须使用
$\u post
或使用CI输入类,如$this->input->post()
,以及url路径使用site\u url()
您的ajax
代码应该如下所示:
$('#drp').change(function(e){
var costcenter = $('#costcenter_id :selected').val();
var location1 = $('#location_id :selected').val();
var department = $('#department_id :selected').val();
$.ajax({
cache: false,
type: 'POST',
data: {'costcenterid':costcenter,'locationid':location1,
'departmentid':department},
url: "<?=site_url('mycontroller/contollerfunction');?>",
success: function(data)
{
alert("success");
}
});
});
注意:请查看浏览器的“网络”选项卡以查看输出:非常感谢。我按照您的建议尝试了,现在我可以在控制器方法中访问所有参数的值。非常感谢您的帮助。您能建议调试PHP程序的最佳方法吗?是否有任何编辑器可用于编写和调试PHP程序?提前谢谢。
public function controllerfunction()
{
$costcenterid = $this->input->post('costcenterid');
$locationid = $this->input->post('locationid');
$departmentid = $this->input->post('departmentid');
$posts = array('costcenterid' => $costcenterid,
'locationid' => $locationid,
'departmentid' => $departmentid);
print_r($posts);die;
}