How to transmit a json Array to php through ajax and insert data into the database through php is as follows. I just started to learn php, and the basics are not good. Baidu has been unable to implement it for a long time. I hope to give an example. Json data is spliced.
Script var fields = "[{\" shop \ ": \" store 1 \ ", \" goods \ ": \" goods 1 \", \ "price \": \ "10 yuan \" },{ \ "shop \": \ "store 2 \", \ "goods \": \ "goods 2 \", \ "price \": \ "11 yuan \" },{ \ "shop \": \ "store 3 \", \ "goods \": \ "goods 3 \", \ "price \": \ "13 yuan \"}] "; $. ajax ({type: "POST", url: "1.php", data: fields, success: function (msg) {alert (msg) ;}}); script t
1. php won't write
Reply to discussion (solution)
Generally, if the format returned by a website is submitted in json POST, it is written as "shop = % E5 % 95% E5 % BA % 86% & goods = % E8 % B4 % A7 % E7 % 971 A91 &..... "(URL encoding)
Get $ shop =$ _ POST ['shop'] in php.
Insert to database
First, your fields is wrong.
JavaScript code is unlikely to be used to assemble json strings. at least AJAX. stringify is used for conversion.
Writing (clear line reading)
Var fields = [{"shop": "shop 1", "goods \": "goods 1", "price": "10 yuan" },{ "shop ": "Store 2", "goods": "goods 2", "price": "11 yuan" },{ "shop": "shop 3", "goods ": "Goods 3", "price": "13 yuan"}];
Submit
data: {fields:fields},
The php $ _ POST ['fields'] is the submitted array.
Generally, if the format returned by a website is submitted in json POST, it is written as "shop = % E5 % 95% E5 % BA % 86% & goods = % E8 % B4 % A7 % E7 % 971 A91 &..... "(URL encoding)
Get $ shop =$ _ POST ['shop'] in php.
Insert to database
The Json Array is used to obtain the table data, without the id or name, and does not know how to form this url encoding.
First, your fields is wrong.
JavaScript code is unlikely to be used to assemble json strings. at least AJAX. stringify is used for conversion.
Writing (clear line reading)
Var fields = [{"shop": "shop 1", "goods \": "goods 1", "price": "10 yuan" },{ "shop ": "Store 2", "goods": "goods 2", "price": "11 yuan" },{ "shop": "shop 3", "goods ": "Goods 3", "price": "13 yuan"}];
Submit
data: {fields:fields},
The php $ _ POST ['fields'] is the submitted array.
How to write php and insert the received json Array into the database?