views:

20

answers:

2

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?

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
    }
A: 

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.

nute
`serialize` does *not* output JSON.
Matthew Flaschen
JSON *is* a serialized object.
Dolph
but then you can easily unserialize in php and then json_encode, no?
nute
+1  A: 

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.

Matthew Flaschen
Thanks, stringify was what I needed. I had never heard of that before.
DavidR