首页 > 解决方案 > 如何使用 Jquery 拆分两个数组并传递给 ajax?

问题描述

插入数据库时​​出现错误。

它的显示列ARRAY。如何解决?而不是我想传递所有日期(在屏幕截图中仅显示日期列中的日期)

我想在代码中知道我做错了什么。为什么数组会这样来?

PHP 模式

$data_jobschedule = array(
            'jobschedule_id' => $jobschedule_id,
            'Activity_area_id' => $Activity_area_id,
            'Contract_id' => $this->input->post('getcontract_id'),
            'job_freq_id' => $this->input->post('getcontractbranch_freq'),
            'job_schedule_dates' => $this->input->post('getschedule'),
            //'job_schedule_frequency' => $this->input->post('getjob_schedule_frequency'),
            'created_at' =>$created_Dt
        );

 $insert_id = 0;
        if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
            $this->db->insert('job_schedule', $data_jobschedule); //HERE IS THE TABLE I NEED TO ADD DATES AND FREQUENCY
            $insert_id = $this->db->insert_id();

        }

标签: phpjqueryarrayscodeigniter

解决方案


所以你的帖子数据是一个数组,你需要遍历它。在你的 php 模型上,尝试改变这个

$data_jobschedule = array(
    'jobschedule_id' => $jobschedule_id,
    'Activity_area_id' => $Activity_area_id,
    'Contract_id' => $this->input->post('getcontract_id'),
    'job_freq_id' => $this->input->post('getcontractbranch_freq'),
    'job_schedule_dates' => $this->input->post('getschedule'),
    //'job_schedule_frequency' => $this->input->post('getjob_schedule_frequency'),
    'created_at' =>$created_Dt
);

$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
    $this->db->insert('job_schedule', $data_jobschedule); //HERE IS THE TABLE I NEED TO ADD DATES AND FREQUENCY
    $insert_id = $this->db->insert_id();
}

$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
    if (is_array($this->input->post('getschedule'))) {
        foreach($this->input->post('getschedule') as $value) {
            $this->db->insert('job_schedule', array(
                'jobschedule_id' => $jobschedule_id,
                'Activity_area_id' => $Activity_area_id,
                'Contract_id' => $this->input->post('getcontract_id'),
                'job_freq_id' => $this->input->post('getcontractbranch_freq'),
                'job_schedule_dates' => $value[0], //assume array form like your screenshot
                'job_schedule_frequency' => $value[1],
                'created_at' =>$created_Dt
            ));
        }

        $insert_id = $this->db->insert_id(); 
    }
}

推荐阅读