我有一个包含动态表行数的表。我试图将所有这些数据放入一个关联数组,然后通过AJAX将其发送到我的数据库。但是它不会插入我的数据库。
这是我的JS和AJAX
$('#submit_button').click(function(){
var table = $("table tbody"),
data_array = [],
counter = 1,
counter_error = 0;
table.find('tr').each(function (i) {
var $tds = $(this).find('td'),
category_dropdown = document.getElementById('category_' +counter),
category_id = category_dropdown.value;
//alert(counter_error);
if(category_id == "0"){
alert('All categories should be filled');
counter_error++;
counter++;
}
else if(category_id == "1" || category_id == "4"){
var item_dropdown = document.getElementById('items_' +counter),
item_id = item_dropdown.value,
item_description = $(item_dropdown).find(':selected').text(),
quantity = document.getElementById('qty_' + counter).value,
jan = document.getElementById('jan_' + counter).value,
feb = document.getElementById('feb_' + counter).value,
mar = document.getElementById('mar_' + counter).value,
apr = document.getElementById('apr_' + counter).value,
may = document.getElementById('may_' + counter).value,
jun = document.getElementById('jun_' + counter).value,
jul = document.getElementById('jul_' + counter).value,
aug = document.getElementById('aug_' + counter).value,
sep = document.getElementById('sep_' + counter).value,
oct = document.getElementById('oct_' + counter).value,
nov = document.getElementById('nov_' + counter).value,
dec = document.getElementById('dec_' + counter).value,
price = document.getElementById('unitprice_' + counter).innerText,
totalqty = 0;
//alert(item_description);
if(quantity == ""){
quantity = 0;
}
if(jan == ""){
jan = 0;
}
if(feb == ""){
feb = 0;
}
if(mar == ""){
mar = 0;
}
if(apr == ""){
apr = 0;
}
if(may == ""){
may = 0;
}
if(jun == ""){
jun = 0;
}
if(jul == ""){
jul = 0;
}
if(aug == ""){
aug = 0;
}
if(sep == ""){
sep = 0;
}
if(oct == ""){
oct = 0;
}
if(nov == ""){
nov = 0;
}
if(dec == ""){
dec = 0;
}
totalqty = parseInt(jan) + parseInt(feb) + parseInt(mar) + parseInt(apr) + parseInt(may) + parseInt(jun) + parseInt(jul) +
parseInt(aug) + parseInt(sep) + parseInt(oct) + parseInt(nov) + parseInt(dec);
//alert(quantity);
if(item_id == "0"){
alert('Please fill-up all item specifications.');
counter_error++;
}
else if(quantity == "0"){
alert('Please specify the amount/quantity for all items');
counter_error++;
}
else if(totalqty != quantity){
alert('All unit quantities should be distributed.');
counter_error++;
}
else{
//put the row element in an data_array
data_array.push({
supply_id : item_id,
supply_description : item_description,
quantity : quantity,
price : price,
jan : jan,
feb : feb,
mar : mar,
apr : apr,
may : may,
jun : jun,
jul : jul,
aug : aug,
sep : sep,
oct : oct,
nov : nov,
dec : dec
});
}
counter++;
}
else{
counter++;
}
});
if(counter_error > 0){
//do not insert to database
}
else{
//insert to database
alert(JSON.stringify(data_array));
//convert data_array to object
var project_details = $.extend({}, data_array);
$.ajax({
type: "POST",
url: "<?php echo base_url('PPMP_controller/submitPPMP'); ?>",
data: { data_array : $.param(project_details) },
dataType: 'json',
success: function(data) {
alert('PPMP submission success!');
},
error: function(errorw) {
alert("error");
}
});
}
});
});
这是我的控制器
public function submitPPMP(){
$data_array = $this->input->post('data_array');
$values = $this->PPMP_model->submitPPMP($data_array);
echo json_encode($values);
}
这是我的模特
function submitPPMP($data_array){
$date_format = 'DATE_W3C';
$date_submitted = standard_date($date_format);
$data = array(
'user_id' => 1,
'date_submitted' => $date_submitted,
'first_lvl_status' => 0,
'second_lvl_status' => 0,
'third_lvl_status' => 0,
'fourth_lvl_status' => 0,
'submitted' => 1
);
$this->db->insert('project', $data);
$id = $this->db->insert_id();
if(is_array($data_array) || is_object($data_array)){
foreach($data_array as $object){
$project_details = array(
'project_id' => $id,
'supply_id' => $object->supply_id,
'supply_description' => $object->supply_description,
'quantity' => $object->quantity,
'price' => $object->price,
'jan' => $object->jan,
'feb' => $object->feb,
'mar' => $object->mar,
'apr' => $object->apr,
'may' => $object->may,
'jun' => $object->jun,
'jul' => $object->jul,
'aug' => $object->aug,
'sep' => $object->sep,
'oct' => $object->oct,
'nov' => $object->nov,
'dec' => $object->dec
);
$this->db->insert('project_details', $project_details);
}
}
}
行$this->db->insert('project', data)
可以使用它来更新我的数据库所需的数据。但是行$this->db->insert('project_details', $project_details)
不会使用我的关联数组中的值更新我的数据库(它甚至根本不更新),即使AJAX请求成功,因为它运行{{1 }}
有人可以帮我解决这个问题我不确定我的控制器/模型是否从AJAX传递给它们的关联数组。但我确定我的关联数组在将success:function(data)
转换为对象之前,其内部的值为seend。
感谢您的帮助。
BTW,我使用Codeigniter 3.0作为MVC框架。