Heavily-scripted data entry forms that expand on-the-fly as the user enters new data make life easier for the user. However, it can be hell for a developer to devise a method for the representation and submission of data with arbitrary length and depth.
A logical thing to do seems to be to take an object-oriented approach to represent all client-side data as one or multiple JavaScript objects. The objects themselves could be arrays or objects with child objects and so on. Of course the objects need to be serialized in some way before they’re posted back. Now, if you’re using PHP at the back-end, you might make use one of the multitude of JSON implementations out there for posting back the data in JSON format and converting the data back to PHP objects (assuming that you want to end up with PHP objects.) JSON is pretty much native to JavaScript (clever use of the toSource method may cut it in most cases) but you need extra PHP code for deserialization.
If you want to take the opposite approach and use PHP-native serialization, then the following function will convert any complex JavaScript object into PHP-serialized string which can be converted back into PHP objects with a single call to the deserialize PHP function. Read the rest of this entry »