我正在尝试通过AJAX将此数组var data_array
传递到我的控制器
当我使用var data_array
alert(JSON.stringify(data_array));
的值
这是我的AJAX方法
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!');
alert(data);
},
error: function(errorw) {
console.log(errorw);
}
});
这是我的PPMP_controller
public function submitPPMP(){
$data_array = $this->input->post('data_array');
$value = $this->PPMP_model->submitPPMP($data_array);
echo json_encode($value);
}
这是我的模特
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)){
return "yes";
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);
}
}
else{
return "no";
}
return $this->db->last_query();
}
然而,从控制器传递到模型的数组不是数组,因为它不通过行if(is_array($data_array) || is_object($data_array))
而是转到else
条件和return "no"
。
可能导致此数组未从控制器传递到模型的数组。感谢您的帮助。我使用Codeigniter 3.0作为MVC框架。
答案 0 :(得分:0)
你将json传递给你的模型而不是数组,当你的模型应该是,
尝试var_dump($data_array)
检查其数据类型,例如json
或object
或array
$data_array = json_decode($data_array); // to get object
$data_array = json_decode($data_array,true); // to get array
您的PPMP_controller
应该是
public function submitPPMP(){
$data_array = $this->input->post('data_array');
$data_array = json_decode($data_array);
$value = $this->PPMP_model->submitPPMP($data_array);
echo json_encode($value);
}