JavaScript - persisting data during user interaction
I am working on a web form maker that uses a mix of server side jQuery and PHP interaction. While the user is creating the form, I am trying to figure out the best method for storing each of the form elements before all the data is sent to the server. I have looked at the following methods.
- Javascript arrays
- XML Document
- Send each form element to the server side for storage in a session
a source to share
The good, the bad and the ugly
Depends on your functionality and application requirements, but Javascript is probably the best way to go. You can use both arrays and objects or whatever in javascript. It is server independent and retains data for a long period of time as long as the client session remains (browser window does not close for any reason), but it can be easily avoided (check my last paragraph).
Using XML documents would be the worst solution because XML is not as well supported on the client side as you might think.
Server side sessions are good and bad. They are great if you save intermediate results from time to time, so if the client session ends for any reason, the user will not lose all data. But the problem is that it can also expire on the server.
If I were you, I would use Javascript storage and, if needed, sometimes send the serialized JSON results to the server and store them there (based on the business process of storing this data somewhere other than a session, might be a better solution ). I would do the second part (with a combination of sides) only if I knew that the user is more likely to create forms in multiple steps over a longer period of time and multiple client sessions. but can also be used to prevent crashes. Anyway. Javascript is your best bet with server side interaction possible .
Saving data between pages on the client
Keep in mind that it also allows you to serve data between pages on the client side. Check out the sessvars library . So even if the page is refreshed or redirected and then returned, it can all be persisted on the client side between these events like magic. Great any little library that has made my life a few times. And significantly reduced the complexity of the application that would otherwise have to be implemented with something more complex.
a source to share
You might want to check out PersistJS , which provides a cross -server persistent storage object. Of course, being persistent , the data stored in this library survives, not just page changes.
The latest version (0.2.0) is here - note that the version in the above link is 0.1.0.
a source to share
Combination # 1 (although I would use objects, not arrays necessarily) and # 3 is a good approach. Saving data locally in the browser (# 1) makes it immediately available. Session-based back-up storage protects you from the page being updated; you can magically restore the page to the way it was.
a source to share