4

I know it is possible to serialize and unserialize in PHP and then have javascript read the response, my question is can I do it the other way around? I have a function that may be called multiple times and each time it is called I need the new data added to an array or an object of some form. Then I want to take all of this data and send it to PHP to be interpreted and analyzed as if it were an array.

I think I am drawing a blank here, and this should be easily done xD Oh yeah and i am storing the information temporarily in a hidden field until it is ready for submission (I will be doing security checks in PHP as well)

Thanks :D

MasterGberry
  • 2,800
  • 6
  • 37
  • 56

2 Answers2

11

To make JavaScript serialize in the syntax of PHP's serialize would require a custom JavaScript function, however you can do what you want with JSON.

To serialize to JSON in JavaScript you would use the stringify method of the JSON object:

JSON.stringify(myArray)

and to unserialize a JSON string in PHP you would use json_decode:

json_decode($myJsonArray)

If you want to support older browsers, you will have to include an external implementation of the JSON object. See Browser-native JSON support

Community
  • 1
  • 1
Stecman
  • 2,890
  • 20
  • 18
0

encode the data with JavaScript into JSON and then post it urlencoded to the server. On the server use json_decode to parse the array.

Korbi
  • 1,438
  • 4
  • 15
  • 22
  • An example could improve your answer. So could a link to the docs for JSON encoding in JS, urlencoding, and `json_decode()`. – Bailey Parker Jun 21 '12 at 03:38