I am trying to use AJAX to send data from a Code Igniter view to a controller that will handle the data as needed. I'm gathering the data using a JQuery plugin (Handsontable) and when the user hits the "save" button it extracts the required data from the table and executes the ajax function.
$.ajax({
url: "/survey/save",
data: {"data": data},
type: "POST",
});
I am able to send it to a regular .php file which collects the data with $_POST but not my controller.
public function save() {
$data = $this->input->post('data');
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
for ($i = 0, $size = count($data); $i < $size; ++$i) {
fwrite($fh, $data[$i][0]."\t".$data[$i][1]."\t".$data[$i][2]."\n");
}
fclose($fh);
}
The above code is not what I really want the controller to do but if can successfully execute this code, I will be able to do what I wish.
I have a feeling it has something to do with the URL of the ajax function but I am extremely new to all of these languages and am probably overlooking something simple. Please let me know if I should include any other code!
Answering my own question in case it helps others. The problem was my csrf settings. I realized turning off csrf protection fixed the problem but I didn't want to keep csrf protection off. I think CI may have came out with a whitelist to fix this but I just edited my config file as follows:
if(stripos($_SERVER["REQUEST_URI"],'/survey') === FALSE)
{
$config['csrf_protection'] = TRUE;
}
else
{
$config['csrf_protection'] = FALSE;
}