Solved

Store data object in cookie and display value

Posted on 2013-06-23
7
606 Views
Last Modified: 2013-06-24
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);
0
Comment
Question by:yando18
7 Comments
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 500 total points
ID: 39269160
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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39269276
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
 

Author Comment

by:yando18
ID: 39269359
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:yando18
ID: 39269467
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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39269551
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39269659
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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39270989
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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question