0

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.

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
Carlos Catalan
  • 71
  • 1
  • 1
  • 4

1 Answers1

0

Pass data like object:

data: {classCode: classCode, students: students},

Edit: Also, googling for existing question under "jquery ajax passing array" would give you this as first link. Check first two answers there too.

Edit: 20160525171759

You can pass JSON stringified array JSON.stringify(array) and manipulate it with json_decode($this->input->post('postKey')).

//JS

//students = ["2014-52307", "2014-26571", "2014-68959", "2014-60379", "2014-56077"];
students = JSON.stringify(students);
//check if same is needed for classCode variable since I don't know if classCode is array too
$.ajax({
    //I removed traditional property to pass an object
    type: 'POST',
    //I put URI as method argument (check if index.php is sufficient)
    url: '<?php echo base_url("index.php/ams_controller/recordAbsence"); ?>', 
    data: {classCode: classCode, students: students}, 
    success: function(resp) { 
        alert("Absences Saved");
    }
});

//PHP
$students = $this->input->post('students');
//echo $students;
Community
  • 1
  • 1
Tpojka
  • 6,996
  • 2
  • 29
  • 39
  • Thanks for the answer, but the data is still not being stored in the database. I checked the two answers in the link you gave me, gave them both a try and it still won't work. – Carlos Catalan May 24 '16 at 15:10
  • Debug `students` variable before ajax (i.e. `console.log(students);`). – Tpojka May 24 '16 at 20:58
  • here's what the console shows for the students before ajax call: ["2014-52307", "2014-26571", "2014-68959", "2014-60379", "2014-56077"] – Carlos Catalan May 25 '16 at 01:18
  • I think you are missing/mixing quotes for URL. Try with: `''` – Tpojka May 25 '16 at 14:23