无法从视图数据传递到控制器
查看代码,一个函数来创建数据的数组,然后把它传递给经由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();
}
这里是你的问题:
data: ({
jfdata: jformdata
})
改变这个
data:{
jfdata: jformdata
}
不工作:(:( – Himanshu
通过不woking是什么意思? – madalinivascu
你的意思是你的ajax isn' t触发,你的意思是你有404 ajax错误,你的意思是你有500eror,你的意思是你有控制台错误,这是什么exacly是“不工作” – madalinivascu
尝试'数据:{jfdata:jformdata},'在你的Ajax查询(我删除了 “()”)。 [jquery ajax](http://api.jquery.com/jquery.ajax/)。也许你需要接收这样的数据:'$ variable = json_decode($ _ POST [“jfdata”]);' – flohdieter
尝试以上所有方法仍然无法正常工作。 – Himanshu
你需要更多的变量的人,这些都不够:)) – madalinivascu