?
Solved

Store data object in cookie and display value

Posted on 2013-06-23
7
Medium Priority
?
611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 2000 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 58

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 58

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 84

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 58

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

765 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