php - Using AJAX to send data to Coder Igniter controller function results in a 500 Internal Server Error -
i trying use ajax send data code igniter view controller handle data needed. i'm gathering data using jquery plugin (handsontable) , when user hits "save" button extracts required data table , executes ajax function.
$.ajax({ url: "/survey/save", data: {"data": data}, type: "post", });
i able send regular .php file collects data $_post not controller.
public function save() { $data = $this->input->post('data'); $myfile = "testfile.txt"; $fh = fopen($myfile, 'w') or die("can't open file"); ($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 not want controller if can execute code, able wish.
i have feeling has url of ajax function extremely new of these languages , overlooking simple. please let me know if should include other code!
hey have change in ajax url
format of url must absolute path e.g.
in view ajax should this
$.ajax({ url:'<?php echo site_url('survey/save'); ?>', type: 'post', data: "data="+data, beforesend:function(){ //before send code e.g. put loader }, success:function(result){ // success result code goes here }, error:function(jqxhr, status, error){ if(status!='error') alert(error); } });
now in controller can data
$data=$this->input->post('data');
and 1 more thing have use relative path when using fopen
e.g.
//you have used
$myfile = "testfile.txt"; //instead of have use $myfile="./your_folder_name/your_file_name";
and can set in config.php
$config['base_url'] = '';
Comments
Post a Comment