How to transmit a json Array to php through ajax and insert data into the database through php

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.