Send json object to mysql using jquery/javascript/php?
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 开发者_如何学Pythonnature, I need all the data in one field in the mysql database. Any ideas?
EDIT: 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
}
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)
{
// ...
}
});
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.
On the server, you use json_decode
to decode, and json_encode
to encode a response.
Maybe you could use the "serialize" concept?
In jQuery: http://api.jquery.com/serialize/
In PHP: http://us.php.net/manual/en/function.serialize.php
So you would take all the input, serialize it via jQuery, send the big string to the server and store in your database as-is? Then later when you retrieve it in php, you can unseralize it.
精彩评论