Solved

Store data object in cookie and display value

Posted on 2013-06-23
7
603 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
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:yando18
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now