Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

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);
0
yando18
Asked:
yando18
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now