Yosef Naser
Yosef Naser

Reputation: 45

Storing cookies for future form submissions

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.

Upvotes: 0

Views: 154

Answers (4)

Bhashit Parikh
Bhashit Parikh

Reputation: 3131

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).

Upvotes: 0

Francisco Zarabozo
Francisco Zarabozo

Reputation: 3748

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.

Upvotes: 1

Dropout
Dropout

Reputation: 13866

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' ) );

Upvotes: 2

Starx
Starx

Reputation: 79049

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

Upvotes: 1

Related Questions