0

Is there any way to store data submitted in cookies and when the same form is loaded again the text fields are filled with the stored values in cookies ?

I have a shopping website and i don't wanna require customers to register, so when they order an item and come back again to order another item the form is prefilled with their information.

I've looked around but no one seems to mention such thing.

Dropout
  • 13,653
  • 10
  • 56
  • 109
Yosef Naser
  • 45
  • 2
  • 8
  • You are correct, you can use persistent cookies to store data. – NullPointerException May 20 '13 at 14:01
  • You should be very careful storing personal information in cookies. – Mike Robinson May 20 '13 at 14:02
  • @MikeRobinson : does this pose a security threat ? I will be storing names, emails and addresses – Yosef Naser May 20 '13 at 14:04
  • @YosefNaser You can encrypt the cookie data. **[Link](http://blog.teamtreehouse.com/how-to-create-totally-secure-cookies)** – NullPointerException May 20 '13 at 14:07
  • @YosefNaser That's definitely an issue. This question points out security issues (http://stackoverflow.com/questions/706858/what-information-is-ok-to-store-in-cookies), but my point is simpler. If your users find out you are storing this information, a vocal minority will eventually become very upset at the concept. Some user's take privacy -very- seriously, and it's not something you should risk on an online store. Finally, isn't this going to interfere with autofill? – Mike Robinson May 20 '13 at 14:19
  • Does any of the answers solve your problem? If so, please mark the answer. Thanks! – Dropout Mar 29 '16 at 08:32

4 Answers4

2

Yes, you can store cookies for later use on the client's browser side. These kind of cookies are called "persistent cookies". They are stored until the client clears his browsing data.

To learn more about them please refer to:

http://www.webopedia.com/TERM/P/persistent_cookie.html

How do I create a persistent vs a non-persistent cookie?

http://en.wikipedia.org/wiki/HTTP_cookie

Have you tried setting the cookie expiration time to a long enough time? For example one year like this?:

setcookie( "myCookie", $myValue, strtotime( '+1 year' ) );
Community
  • 1
  • 1
Dropout
  • 13,653
  • 10
  • 56
  • 109
1

You can use $_COOKIE[] to access and retrieve information stored in cookie.

Starx
  • 77,474
  • 47
  • 185
  • 261
  • Yes, but the regular kind of cookies have a certain "lifespan" after which they expire.. You have to use persistent cookies in order to do something like the OP requires. – Dropout May 20 '13 at 14:06
1

That's a very obsolete way to accomplish what you need, it's prone to error and privacy issues. Terrible idea on any computer that can be shared.

If you want to do something like that, then create a single persistent cookie with a session ID. Then identify your customer with that session ID and store/read all the data from a database using that session ID as a reference.

Also, give your customer clear knowledge on the fact that you are using a persistent cookie to remember his data, and give him a button with a "don't remember my data" option.

Francisco Zarabozo
  • 3,676
  • 2
  • 28
  • 54
0

Add an onsubmit event listener on the form. Read the fields in the form using whichever way you are comfortable with. Now, as soon as the form is submitted, you can do something like following: (just an example, using jQuery-cookie)

$('#yourFormId .fieldClass').each(function() {
    // a cookie that expires in 30 days.
    $.cookie('somePrefix' + $(this).attr('id'), $(this).val(), {expires: 30});
});

On the next, page-load:

$(function() {
     $('#yourFormId .fieldClass').each(function() {
         var value = $.cookie('somePrefix' + $(this).attr('id'));
         $(this).val(value);
     });
});

Even better, whichever server-side language language you might be using, will allow you to access the cookie-values on the next page-load. So, you could directly fill-in the form-fields by using the cookies. For ex: if you are using PHP, you might do the following:

<input id="someId" value="<%php echo $_COOKIE['somePrefixsomeId']; %>" />

Although storing user-names or emails might be okay in such a way, please do not store any authentication information in cookies (such as passwords), or any other sensitive information (such as credit-card numbers).

Bhashit Parikh
  • 3,121
  • 23
  • 24