1

I have a javascript application that's will keep track of the x&y coordinates of several objects on a 'canvas.' I'm planning on storing this data in a 2-dimensional array such as:

new Array(
    new Array(id0, x0, y0),
    new Array(id1, x1, y1),
    new Array(id2, x2, y2));

I want to submit this data to a php script and store it in a mysql database for later retrieval/redisplay. What's a good way to do this? Should I serialize the array and encode it for transport? I'm using jQuery so if anyone knows of any good functions included in this framework that would be great. Thanks!

Casey Flynn
  • 13,654
  • 23
  • 103
  • 194

2 Answers2

2

The best idea seems to be JSON.

Just encode your array in JSON, send it to the server. If you do not need to manipulate data on the server side, you can even save the JSON itself to MySQL. Otherwise, all wide-spread server-side languages have JSON handling functions (json_decode() and friends in PHP).

On the route back (from server to Javascript), JSON again is the simplest way. You can use jQuery's getJSON() or parseJSON() functions to handle it, and get your array.

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
2

Serialize your array to a storable format (usually string), send it to the server that will store it to the DB.

You might use JSON, XML or any other format. I'd go for JSON as it's less verbose, then using less space in your database.

However be aware that serializing arrays prevent any querying using the database engine. You will have to extract your data, unserialize it, check if it's the one you need, and rinse and repeat until you found the right array (unless you know the ID of the array you're looking for).

NB: Don't forget to use POST request to send it to the server, as GET request have a length limit.

Clement Herreman
  • 10,274
  • 4
  • 35
  • 57