查看代码,一个创建数据数组的函数,然后通过AJAX将其传递给控制器:
function jobsheet_save() {
// array for parts
var parts = new Array(c);
for (i = 0; i < c; i++)
parts[i] = new Array(4);
for (i = 0; i < c; i++) {
parts[i][0] = $('#textbox' + (i+1)).val();
parts[i][1] = $('#pd' + (i+1)).val();
parts[i][2] = $('#pq' + (i+1)).val();
parts[i][3] = $('#pa' + (i+1)).val();
}
var jformdata = new Array();
// array for form
var jid = $('#jobsheetid').val();
var jregdate = $('#regdate').val();
var jregby = $('#regby').val();
var jsource = $('#source').val();
var jcompdate = $('#completiondate').val();
var jdeldate = $('#deliverydate').val();
var jjcustid = $('#jcustomerId').val();
var jcustname = $('#jcustomername').val();
var jcustadd = $('#jaddress1').val();
var jcustphone = $('#jphone1').val();
var jcustemail = $('#jemail').val();
var jproducttype = $('#jproducttype').val();
var jproductname = $('#jproductname').val();
var jproductmodel = $('#pmodel').val();
var jproductserial = $('#pserialno').val();
var jproductsubcat = $('#pscat').val();
var jproductwarranty = $('#pwarranty').val();
var jproductcondition = $('#pcondition').val();
var jproductcomplaint = $('#jproductcomplaint').val();
var jproductcomptype = $('#jcomplainttype').val();
var jproductpurdate = $('#pdate').val();
var jdealername = $('#jdealername').val();
var jdealerphone = $('#jdealerphone').val();
var jcallallocdate = $('#jcallallocdate').val();
var jcallalloctime = $('#jcallalloctime').val();
var jvisitdate = $('#visitdate').val();
var jvisitdetails = $('#visitdetails').val();
var jengname = $('#engname').val();
var jrepairremarks = $('#repairremarks').val();
// insert variable values into array
jformdata[0] = jid;
jformdata[1] = jregdate;
jformdata[2] = jregby;
jformdata[3] = jsource;
jformdata[4] = jcompdate;
jformdata[5] = jdeldate;
jformdata[6] = jjcustid;
jformdata[7] = jcustname;
jformdata[8] = jcustadd;
jformdata[9] = jcustphone;
jformdata[10] = jproducttype;
jformdata[11] = jproductname;
jformdata[12] = jproductmodel;
jformdata[13] = jproductserial;
jformdata[14] = jproductsubcat;
jformdata[15] = jproductwarranty;
jformdata[16] = jproductcondition;
jformdata[17] = jproductcomplaint;
jformdata[18] = jproductcomptype;
jformdata[19] = jproductpurdate;
jformdata[20] = jdealername;
jformdata[21] = jdealerphone;
jformdata[22] = jcallallocdate;
jformdata[23] = jcallalloctime;
jformdata[24] = jvisitdate;
jformdata[25] = jvisitdetails;
jformdata[26] = jengname;
jformdata[27] = jrepairremarks;
alert(jformdata);
// ajax adding data to database
$.ajax({
url: "<?php echo site_url('jobsheet_controller/add_data')?>",
type: "POST",
data: ({
jfdata: jformdata
}),
success: function (data) {
alert("aala r");
if (data.status) { // if success close modal and reload ajax table
$('#modal_jobsheet').modal('hide');
reload_table();
} else {
for (var i = 0; i < data.inputerror.length; i++) {
$('[name="' + data.inputerror[i] + '"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="' + data.inputerror[i] + '"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Error adding / update data');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
}
});
}
这是从视图接受数据然后将其传递给模型的控制器代码:
public function add_data() {
$dealercode = $this->session->userdata('userid');
$did = $dealercode['dealercode'];
$data = $this->input->post('jfdata');
$data3 = array(
'jobsheetid' => $data[0],
'regdate' => $data[1],
'regby' => $data[2],
'source' => $data[3],
'completiondate' => $data[4],
'deliverydate' => $data[5],
'customerId' => $data[6],
'customername' => $data[7],
'customeraddress' => $data[8],
'customerphone' => $data[9],
'producttype'=> $data[10],
'productname' => $data[11],
'productmodel' => $data[12],
'productserial' => $data[13],
'subcategory' => $data[14],
'warranty' => $data[15],
'productcondition' => $data[16],
'complaint' => $data[17],
'complainttype' => $data[18],
'purchasedate' => $data[19],
'dealername' => $data[20],
'dealerphone'=> $data[21],
'callallocdate' => $data[22],
'callalloctime' => $data[23],
'visitdate' => $data[24],
'visitdetail' => $data[25],
'engname' => $data[26],
'repairremark' => $data[27],
'servicecharges' => $data[28],
'dealercode' => $did
);
// Save data to customerMaster
$insert = $this->jobsheet->save($data3);
echo json_encode(array("status" => TRUE));
}
这是模型代码,一种将数据发送到数据库的功能:
public function save($data) {
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
答案 0 :(得分:0)
在ajax函数中添加数据类型为&#39; json&#39;或者&#39; html&#39;。
答案 1 :(得分:0)
这是你的问题:
data: ({
jfdata: jformdata
})
更改为此
data:{
jfdata: jformdata
}