views:

475

answers:

6

I have an interactive web application powered by jQuery where users can manipulate visual objects on the screen. When done, the "state" of JavaScript objects should be sent to PHP to store into database. I'd prefer to use GET for this, but solution that uses POST to submit the data is also viable.

I'm currently thinking to serialize all JS objects using something like base64 encoding and just use something like:

var str = encode_objects();
document.location = 'result.php?result='+str;

However, something tells me there has to be some, more elegant, way than writing my own base64 encoding function in JavaScript. Isn't there something already built into JavaScript that can do the job?

Update: decoding in PHP is not the problem. I know how to decode base64, JSON, whatever in PHP. The problem is how to encode data on JavaScript side.

AJAX is out of the question. It has to be a clean GET or POST with page reload.

+3  A: 

first of all, if you're updating the objects state, it should be a POST. try to keep all your GET idempotent.

second, even if you don't want to do any AJAX, JSON is still your friend. the easiest way would be to serialize your objects into JSON and send the resulting string in the POST dataload. there are many ways to decode JSON in PHP, and you're all set.

Javier
At least, json_decode (since PHP 5.2) ; Zend_Json can also be used as a standalone component (I've used it on a PHP 5.1 server)
Pascal MARTIN
@Milam, json_decode() or Zend_Json::decode()/
Ionuț G. Stan
http://gggeek.altervista.org/sw/article_20061113.html
Ben
Ok, I guess this answer is almost acceptable. But, could you explain how do I "serialize my objects into JSON"?
Milan Babuškov
+2  A: 

You can use this serialize function, and then unserialize it on the PHP end.

jimyi
+1  A: 

GET or POST will probably depend on the size of your data : there is a limit on the amout of data you can pass through GET (something like 2000 bytes ; more, depending on the browser).

There is also an other thing to take into account : if you are modifying data, you should use POST ; not GET, which is used to... get... data.

About the format, I would really not go for anything like a custom function doing any kind of stuff like base64 : I would definitly go for JSON, which is a native Javascript notation.

You can for instance have a look at :

There are many JS libraries that can generate JSON (probably every JS Framework can ; and there are standlone libraries like this one) ; and since PHP 5.2 there are functions in PHP to encode/decode it (see json_encode and json_decode)

Pascal MARTIN
I know all about JSON. But "many libraries" is not the answer I was looking at. Please name one concrete that YOU are using.
Milan Babuškov
I generally use Prototype as JS Framework, which gets me more thah "just" JSON support (see http://www.prototypejs.org/learn/json for some explanations about that).I've also worked with jQuery, and it's toJSON plugin (see http://docs.jquery.com/Plugins).
Pascal MARTIN
A: 

I've used Jquery/JSON.

var jsonArray = $.toJSON(jsArray);

Then sent it via JQuery/Ajax

Ahmet Kakıcı
+1  A: 

json_decode() should serve you well here.

If you simply append a string containing the JSON representation of your javascript object to your HTTP request, you can json_decode() it in PHP and have a PHP object ready to go.

Edit: I should mention that this page has a link to a Javascript JSON stringifier, which will convert your JS objects into the necessary JSON.

zombat
+1  A: 

If it's just a simple flat array you don't need to do anything fancy, as PHP has a built in feature to parse array syntax from GET/POST variable names. Rough example below.

Javascript side:

// Do the parameter-building however you want, I picked the short/messy way
var arrayvalues = [1, 2, 'a'];
var querystring = "var[]=" + arrayvalues.join("&var[]=");
querystring += "&var[something]=abcdef";
// querystring is now "var[]=1&var[]=2&var[]=a&var[something]=abcdef"

PHP side:

var_dump($_POST);
// Remember to validate the data properly!
if ( is_array($_POST['var']) ) {
    count($_POST['var']);
    echo $_POST['var']['something'];
    array_map('do_something_interesting', $_POST['var']);
}
Ant P.