Reputation: 19
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.
My Ajax Code
<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
Upvotes: 1
Views: 1914
Reputation: 2355
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);
}
Upvotes: 1
Reputation: 16117
In your case, you can pass 2 values in params as like:
data: { key1: value1, key2: value2 }
And you can get them in your controller by using $_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.
In your controller file, you can simple get the values in $_POST
, you can check both values by using:
echo '<pre>';
print_r($_POST);
You will get your both values in $_POST
then you can access by using $_POST['key1']
or $_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 section = $('.section').val();
data: 'class='+xclass+'§ion='+section, // here you can use quote and & for separation.
Upvotes: 3