I'm trying to post a javascript array to a controller function in codeigniter then store it into the database via AJAX. I have no problem when passing single values, and this is the first time i'm passing an array.
Here's the code
//JAVASCRIPT IN THE VIEW (students variable already populated and classCode already has a value)
$.ajax({
traditional: true,
type: 'POST',
url: '<?php echo base_url(); ?>index.php/ams_controller/recordAbsence',
data: 'classCode='+classCode+'&students='+students,
success: function(resp) {
alert("Absences Saved");
}
});
//CONTROLLER FUNCTION
public function recordAbsence() {
$temp=getdate(date("U"));
$date = $temp[month] ." ". $temp[mday] ." ". $temp[year];
$classCode = $this->input->post('classCode');
$students = $this->input->post('students');
$this->load->model('model_users');
if($this->model_users->recordAbsence($classCode, $students, $date)) {
return true;
} else{
return false;
}
}
//MODEL FUNCTION
public function recordAbsence($classCode, $students, $date) {
foreach($students as $row) {
$data = array(
'stud_no' => $row,
'date_of_absence' => $date,
'classCode' => $classCode
);
$query = $this->db->insert('absence', $data);
}
if($query) {
return true;
} else {
return false;
}
}
The data isn't being stored in the absence table. Any help would be appreciated.