使用jQuery/JavaScript/PHP将JSON对象发送到mysql? [英] Send json object to mysql using jquery/javascript/php?
问题描述
我有一个页面,该页面从多个字段生成json数据,我想将其发送到mysql数据库. 我当前有一个链接,用户单击该链接,javascript将获取他们输入的信息并将其发送到服务器.由于这种性质,我需要将所有数据放在mysql数据库的一个字段中.有什么想法吗?
I have a page that generates json data from several fields and I want to send it to a mysql database. I currently have a link, the user clicks the link, the javascript takes the information they've inputed and sends it to a server. Due to the nature, I need all the data in one field in the mysql database. Any ideas?
数据来自多个可内容编辑的div,以这种方式存在特定的层次结构. 如
The data comes from several contenteditable divs, in such a way that there is a specific hierarchy. Such as
Data : {
Heading : 1,
info1 : 1,
info2 : 2,
info3 : 3
},{
Heading : 2,
info1 : 1,
info2 : 2,
info3 : 3
}
推荐答案
您可以使用 AJAX 发布.例如:
You can use a AJAX post. E.g.:
$.ajax({
url: someURL,
type: 'post',
data: JSON.stringify(myObj),
contentType: 'application/json',
dataType: 'json',
success: function(data, status, xhr)
{
// ...
}
});
要确保您具有JSON.stringify,请使用 json2 一个>. contentType
表示您正在发布JSON文档,dataType
表示您希望从服务器收到一个文档.
To make sure you have JSON.stringify, use json2. The contentType
means you are posting a JSON document, the dataType
means you expect to receive one from the server.
在服务器上,您使用 json_decode
进行解码,然后 json_encode
来对响应进行编码.
On the server, you use json_decode
to decode, and json_encode
to encode a response.
这篇关于使用jQuery/JavaScript/PHP将JSON对象发送到mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!