Jump to content

Passing Object B/w Javascript And Php


nachumk
 Share

Recommended Posts

I have been looking around and I can't seem to find any simple way of passing arrays of objects between js and php. I have already started encoding my object array into field=val&.... post arrays and I'm passing the information this way.Is there a better way? I've read about XML but I don't see any native support in both js and php for encoding and decoding. The same issue exists for JSON.What am I missing?What I'd most like is to have an object itemArray and pass it string encoded to myXHR.send() function. And on callback have some function to decode xhr.responseText back to an object. Does something standard exist?Thanx,

Link to comment
Share on other sites

I will get told off for going on about it, but jQuery has pretty good JSON support and PHP can encode and decode it pretty easily.

Link to comment
Share on other sites

JavaScript has native JSON support (e.g. you can just write var a = {"c" : 10, "b" : function () { alert("B"); }}; and it will understand), and PHP has the json_encode() and json_decode() functions, so JSON is a fairly good option.

Edited by Synook
Link to comment
Share on other sites

I know that this is an impolite answer but I don't want any 3rd party libraries. It's part of challenging myself to learn the language as it stands today. I definitely don't want to debate the many disadvantages to my view, and I know that with the default installations I am probably using some 3rd party stuff without knowing... but that's how it is.Is there a more standard way of dealing with information transfer b/w js and php? arrays of objects and the like?Thank you,

Link to comment
Share on other sites

You just use the JSON.stringify() method (native to JavaScript) to convert the object to a JSON string, and send that. Then you use json_decode() on the PHP side to convert it back to an object.Note: this is about as "standard" as you can get. You don't need any sort of external library.

Edited by Synook
Link to comment
Share on other sites

This function doesn't exist, when I try to call it the script dies. Perhaps I need to enable something?I wrote this in a called function: var a = {"c" : 10}; var b = "test"; var b .= a.stringify(); alert(:);The alert doesn't come up. If i comment out the stringify the code continues, and the alert comes up. This means to me that stringify is invalid. (Perhaps someone could point me to a more intelligent way of debugging :/ ...

Link to comment
Share on other sites

It's a static method (because Object doesn't descend from JSON it can't inherit it (have you read up on object-oriented programming yet?)) (also, the string concatenation operator in JavaScript is +) (also, you can't declare b twice, you have to lose the var on the second assignment):

var a = {"c" : 10};var b = "test";b += JSON.stringify(a);alert(b);

For debugging, get Firebug for Firefox.

Edited by Synook
Link to comment
Share on other sites

Have you looked at the JSON site (http://json.org/)? It's a great resource for everything JSON, including examples in virtually every language you can think of.Note that because JS is a prototype-based language JSON.stringify() isn't technically a static method (because there is no such thing), however you don't need to clone the JSON object to use it, so it is useful to just think of it as a static method. Also, while static methods are sometimes known as class methods (as opposed to instance methods) that terminology is less widely used.

Edited by Synook
Link to comment
Share on other sites

So I've definitely visited JSON.org before and still I didn't notice it. I just went back to see why. The link from JSON.org to their page js.html has the word (Chinese) next to it. This is probably the reason why I didn't open that page up.You've been a great help as my manual implementation felt wrong and I will be happy to switch it to JSON now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...