Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize/Unserialize javascript and be read by PHP

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

like image 845
MasterGberry Avatar asked Dec 20 '22 20:12

MasterGberry


1 Answers

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

like image 183
Stecman Avatar answered Dec 31 '22 05:12

Stecman