Store data object in cookie and display value

Hello,

I have a form where the input field values are stored in a data object. I need to store the data object values into a cookie that I can then read an inject the values back into the fields if the form page is reloaded.

The data object looks like this:

var myObjData = {            

        "title": "",
        "description": "",         
        "address1": "",
        "address2": null,
        "city": "",
        "state": "",
        "zip": "",
        "categories": [
            {
                "id": 1,
                "name": ""
            }
        ],
        "amount": {
            "id": 1,
            "createdTs":null,
            "goal":null,
            "endTs": null
        },
        "help": [
            {
                "id": 1,
                "createdTs":null,
                "endTs": null,
                "address1": "",
                "address2": null,
                "city": "",
                "state": "",
                "zip": "",
                "need": "",
                "description": "",
                "amount": ""
            }
        ],
};

Open in new window


I assume the values would have to be turned into a string then stored as a cookie which could then be injected into the values from which they came.

var cookie = JSON.stringify(myObjData);
yando18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jagadishwor DulalBraces MediaCommented:
As you are using json data I suggest you to use localstorage.
http://coding.smashingmagazine.com/2010/10/11/local-storage-and-how-to-use-it/
and you can get jStorage js here
http://www.jstorage.info/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
It would be preferable to save that data in a database and then just store a unique key in the cookie.

1. Security - you are putting sensitive data into a cookie
2. Practicality - managing all that data inside a cookie is unnecessary - much easier to store it server side and just put a unique ID in the cookie - this is how it is done most of the time.
0
yando18Author Commented:
The issue is that it's a multi step form and if the page is reloaded data is lost. Since most of the data will be incomplete it can't be stored on the backend.

Is there no way to store the progress in the session and place the data into the fields if page is reloaded.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

yando18Author Commented:
This is the code I have working I just need help injecting values back into the form feilds from which they came.

var dataStore = $.cookie("store-data", JSON.stringify(myObjData));

var parseData = JSON.parse($.cookie("store-data"));

Open in new window

0
Julian HansenCommented:
You can either store in the session or store in the database linked to the session. The latter would be if you want the data to persist beyond the normal expire time of a session.

It does not matter the form is partially complete - many shopping cart systems work on a similar method where a cookie on your machine links you to your open cart - so when you return even though you are not registered with the site it can find your cart.

You should consider using a similar approach here.
0
Dave BaldwinFixer of ProblemsCommented:
Cookies may be limited to only 4KB of data each.  http://en.wikipedia.org/wiki/HTTP_cookie#Implementation  A database is a much better method of saving data along the way as shown by shopping carts.  I am certain that multi-page forms do the same thing.  In shopping carts, there are multiple entries that are organized under a single ID.  No reason you couldn't do the same thing for your multi-page form and then use all the entries to make the report when it is done.
0
Julian HansenCommented:
I don't think this was the correct answer to this post.

You are potentially storing sensitive data in local storage which is visible to anyone who has access to the machine.

As I understand it the local storage facility should be used to cache data that is used in page but takes a relatively long time to get or to store information for the UI that is not sensitive.

I stand to be corrected but it seems to me that with identity theft rife you are potentially exposing yourself to repurcussions from the people who use your site if it transpires you are storing their details in a location that can be accessed by other people or scripts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.