我想问一下,如何从CodeIgniter RESTful Server上的数组中的序列化数据接收数据?
的Ajax
$.ajax({
type: 'POST',
url: "/mywebsite/api/index.php/purchase",
data: $("#formdatatrans").serialize(),
success: function(response) {
alert("Success");
}
});
Html表格
<input type="text" name="data1[]">
<input type="text" name="data2[]">
<input type="text" name="data3[]">
<input type="text" name="data4[]">
控制器
function index_post() {
$data = array(
'data1' => $this->post('data1'),
'data2' => $this->post('data2'),
'data3' => $this->post('data3'),
'data4' => $this->post('data4'));
$insert = $this->db->insert('table1', $data);
if ($insert) {
$this->response($data, 200);
} else {
$this->response(array('status' => 'fail', 502));
}}
输入数据
Array [0]
Data1 = abc_1
Data2 = abc_1
Data3 = abc_1
Data4 = abc_1
Array [1]
Data1 = abc_2
Data2 = abc_2
Data3 = abc_2
Data4 = abc_2
如何使用for / while循环一次性添加上述所有数据?
提前致谢
答案 0 :(得分:0)
您可以将合并与合并一起使用,您可以将两个数组转换为数组
<?php
$array1 = array('Data1' =>"abc_1",'Data2' =>"abc_1",'Data3' =>"abc_1",'Data4' =>"abc_1");
$array2=array('Data5' =>"abc_2",'Data6' =>"abc_2",'Data7' =>"abc_2",'Data8' =>"abc_2");
$data = array_merge($array1, $array2);
print_r($output);
?>
答案 1 :(得分:0)
用这个替换你的控制器并检查这将帮助你使用foreach循环一次插入。
function index_post() {
$data1 = $this->post('data1');
$data2 = $this->post('data2');
$data3 = $this->post('data3');
$data4 = $this->post('data4');
foreach ($data1 as $key=> $value) {
$data['data1'] = $data1[$key];
$data['data2'] = $data2[$key];
$data['data3'] = $data3[$key];
$data['data4'] = $data4[$key];
$insert = $this->db->insert('table1', $data);
}
if ($insert) {
$this->response($data, 200);
} else {
$this->response(array('status' => 'fail', 502));
}
}