[英]How Can I pass two variable in Ajax URL to codeigniter controller?
I am working on a School Management system. 我正在使用学校管理系统。 I am just creating fetch student details by their class and section. 我只是按班级和班级创建获取学生的详细信息。 I use ajax + codeigniter controller but I am unable to pass two variable in ajax call to perform 2 parameter search. 我使用ajax + codeigniter控制器,但是无法在ajax调用中传递两个变量来执行2参数搜索。
My Ajax Code 我的Ajax代码
<script>
$(document).ready(function () {
$('#example').DataTable({
'paging': true,
'searching': true,
'ordering': true,
'autoWidth': false
});
$('#student').click(function (event) {
event.preventDefault();
var xclass = $('.sclass').val();
var section = $('.section').val();
//
$.ajax({
url: "<?php echo base_url(); ?>Admission/fetchStudent/",
type: "POST",
data: {'xclass': xclass, 'section':section},
datatype: 'json',
success: function (data) {
$("#resultlist").html(data);
}
});
});
}); //event.preventDefault();
</script>
My Controller 我的控制器
public function fetchStudent($class,$section){
$this->load->model('Admission_model');
$data = $this->Admission_model->fetchStudentmodel($class,$section);
echo '<pre>';
print_r($data);
exit();
echo json_encode($data);
}
My Model is 我的模特是
public function fetchStudentmodel($x,$y) {
$uid = $this->session->userdata('user_id');
$data = $this->db->select('*')
->from('student')
->where(['user_id' => $uid,'class'=>$x, 'section'=>$y])
->get();
if ($data->num_rows() > 0) {
return $data->result();
} else {
return 'No data Available';
}
}
See this image then you can understand what I want to do 看到这张图片,您就可以了解我想要做什么
In your case, you can pass 2 values in params as like: 在您的情况下,您可以在参数中传递2个值,例如:
data: { key1: value1, key2: value2 }
And you can get them in your controller by using $_POST
您可以使用$_POST
将它们放入控制器中
But in your example, you are sending only 1 param and not getting it in your controller, your are using your URL slug for getting class. 但是在您的示例中,您仅发送1个参数,而未在控制器中获取它,而是使用URL子句获取类。
In your controller file, you can simple get the values in $_POST
, you can check both values by using: 在您的控制器文件中,您可以简单地获取$_POST
的值,可以使用以下方法检查这两个值:
echo '<pre>';
print_r($_POST);
You will get your both values in $_POST
then you can access by using $_POST['key1']
or $_POST['key2']
您将在$_POST
获得两个值,然后可以使用$_POST['key1']
或$_POST['key2']
One more thing, i dont know why are you using single quote on your param's key, this will be converted in a string variable i think. 还有一件事,我不知道您为什么在参数的键上使用单引号,这会转换为我认为的字符串变量。
Second solution for your example is: var xclass = $('.sclass').val(); 您的示例的第二个解决方案是:var xclass = $('。sclass')。val(); var section = $('.section').val(); var section = $('。section')。val();
data: 'class='+xclass+'§ion='+section, // here you can use quote and & for separation.
Try This, 尝试这个,
$.ajax({
url: "<?php echo base_url(); ?>Admission/fetchStudent/",
type: "POST",
data: ({xclass: xclass, section:section}),//changes
datatype: 'json',
success: function (data) {
$("#resultlist").html(data);
}
});
public function fetchStudent(){//changes
$this->load->model('Admission_model');
$class = $this->input->post('xclass');//changes
$section = $this->input->post('section');//changes
$data = $this->Admission_model->fetchStudentmodel($class,$section);
echo '<pre>';
print_r($data);
exit();
echo json_encode($data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.