怎么通过ajax传送json数组到php,并通过php将数据插入数据库

WBOY
Release: 2016-06-23 13:17:51
Original
1199 people have browsed it

如题。本人刚开始学习php,基础并不好,百度了好久都无法实现,希望能给出例子。json数据是拼接得到的

<script>var fields = "[{ \"shop\":\"商店1\" , \"goods\":\"货物1\",\"price\":\"10 元\" },{\"shop\":\"商店2\" , \"goods\":\"货物2\",\"price\":\"11 元\" },{ \“shop\":\"商店3\" , \"goods\":\"货物3\",\"price\":\"13 元\”}]";  $.ajax({        type: "POST",        url : "1.php",        data: fields,        success: function(msg){          alert(msg);        }      });</script>t
Copy after login

1.php不会写


回复讨论(解决方案)

一般网站返回的格式是json POST提交的话 写成 “shop=%E5%95%86%E5%BA%971&goods=%E8%B4%A7%E7%89%A91&.....”(URL编码)

php里再获取 $shop= $_POST['shop'];
插入到数据库

首先你的 fields 写错了
不大可能用 js 代码去拼装 json 串,至少也是用 AJAX.stringify 方法来转换
要写作(断行看的清楚点)

var fields = [  { "shop":"商店1" , "goods\":"货物1","price":"10 元" },  { "shop":"商店2" , "goods":"货物2","price":"11 元" },  { “shop":"商店3" , "goods":"货物3","price":"13 元”}];
Copy after login
Copy after login

提交时
data: {fields:fields},
Copy after login
Copy after login


php 端 $_POST['fields'] 就是提交的数组

一般网站返回的格式是json POST提交的话 写成 “shop=%E5%95%86%E5%BA%971&goods=%E8%B4%A7%E7%89%A91&.....”(URL编码)

php里再获取 $shop= $_POST['shop'];
插入到数据库


Json数组是获取table的数据拼接的,没有id,name之类的不知道怎么形成这种url编码

首先你的 fields 写错了
不大可能用 js 代码去拼装 json 串,至少也是用 AJAX.stringify 方法来转换
要写作(断行看的清楚点)

var fields = [  { "shop":"商店1" , "goods\":"货物1","price":"10 元" },  { "shop":"商店2" , "goods":"货物2","price":"11 元" },  { “shop":"商店3" , "goods":"货物3","price":"13 元”}];
Copy after login
Copy after login

提交时
data: {fields:fields},
Copy after login
Copy after login


php 端 $_POST['fields'] 就是提交的数组


php怎么写,怎么将接收过来的json数组插入数据库?
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!