0

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
    }
1
  • At some point in the future, you or someone you love will realize that storing "all the data in one field in the mysql database" was a Bad Idea. Commented Jul 8, 2010 at 0:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, stringify was what I needed. I had never heard of that before.
0

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.

3 Comments

serialize does not output JSON.
JSON is a serialized object.
but then you can easily unserialize in php and then json_encode, no?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.