在codeIgniter中如何从ajax post中提取序列化数组?

时间:2018-06-08 11:24:43

标签: php jquery ajax codeigniter

在CodeIgniter中如何从ajax发布json解码序列化数组?

我想基于property_id和item_definition_id存储这些细节。  这两个服务的property_id相同。 item_definition_idservice_name不同。

在这个项目中,我有4种类型的服务。维护,add_on,着陆和清洁。在此服务类型下方,有5-10个服务名称。

我想在user(property_id)选择服务时保存这些详细信息,然后点击保存按钮(保存按钮位于表单之外)

从控制台,我得到了这样的序列化数组。

0:{name: "service_type", value: "main"}
1:{name: "service_name", value: "Maintenance"}
2:{name: "unit", value: "Per Visit"}
3:{name: "item_definition_id", value: "26"}
4:{name: "visits", value: "14"}
5:{name: "extra_charge", value: ""}
6:{name: "override_charge", value: "3600"}
7:{name: "maintenance.level1.type1", value: ""}
8:{name: "maintenance.level1.type2", value: "1"}
9:{name: "maintenance.level1.type3", value: "1"}
10:{name: "maintenance.level1.type4", value: ""}
11:{name: "maintenance.level1.type5", value: "6"}
12:{name: "maintenance.level2.type1", value: "4"}
13:{name: "maintenance.level2.type2", value: ""}
14:{name: "maintenance.level2.type3", value: ""}
15:{name: "maintenance.level2.type4", value: "4"}
16:{name: "maintenance.level2.type5", value: "1"}
17:{name: "maintenance.level3.type1", value: ""}
19:{name: "maintenance.level3.type2", value: ""}
20:{name: "maintenance.level3.type3", value: "1"}
21:{name: "maintenance.level3.type4", value: "1"}
22:{name: "maintenance.level3.type5", value: "4"}
23:{name: "maintenance.level4.type1", value: ""}
24:{name: "maintenance.level4.type2", value: ""}
25:{name: "maintenance.level4.type3", value: ""}
26:{name: "maintenance.level4.type4", value: ""}
27:{name: "maintenance.level4.type5", value: ""}
28:{name: "maintenance.level5.type1", value: ""}
29:{name: "maintenance.level5.type2", value: ""}
30:{name: "maintenance.level5.type3", value: ""}
31:{name: "maintenance.level5.type4", value: ""}
32:{name: "maintenance.level5.type5", value: ""}
33:{name: "contract_notes", value: "4 Hour monthly visit"}
34:{name: "internal_notes", value: ""}
35:{name: "service_type", value: "add_on"}

36:{name: "service_type", value: "add_on"}
37:{name: "service_name", value: "Gutter Cleaning"}
38:{name: "unit", value: "Per Visit"}
39:{name: "item_definition_id", value: "19"}
40:{name: "visits", value: "1"}
41:{name: "gutter_cleaning_linear_feet", value: ""}
42:{name: "gutter_cleaning_greater_than_15_feet", value: ""}
43:{name: "gutter_cleaning_cannot_walk_roof", value: ""}
44:{name: "extra_charge", value: ""}
45:{name: "override_charge", value: "348"}
46:{name: "contract_notes", value: ""}
47:{name: "internal_notes", value: ""}
48:{name: "property_id", value: "173"}
49:{name: "contractDate", value: "05/02/2019"}
50:{name: "contract_id", value: "1473"}
51:{name: "duration_months", value: "12"}
控制器功能

中的

public function save_contracts(){    

    $data = json_decode($_POST['data'], true);
    $newArr = [];
    foreach ($data as $arr) {
        foreach ($arr as $k => $v) {
            if($k == 'name') $name = $v;
            if($k == 'value') $value = $v;
        }
        $newArr[ $name ] = $value;
    }
    $UpdateItems =  $this->current_model->UpdateContracts($newArr);
    //echo $UpdateItems;

} 

这里我得到了最后一个service_name。

1 个答案:

答案 0 :(得分:1)

您可以使用$postData = $this->input->post();发帖

$getData = $this->input->get();

并访问每个fild $post['service_type]

https://www.codeigniter.com/user_guide/libraries/input.html?highlight=post#CI_Input::get_post