...

View Full Version : Cookies not saving after closing out of browser



nicky
10-10-2011, 08:06 PM
I'm trying to get my form to save a user's data via cookies. I found a script at http://www.fijiwebdesign.com/blog/remembering-form-input-data-with-javascript-and-cookies-before-submission.html, and it only seems to work if you refresh the page, but not if you close out the browser and restart.



<form action="process-contact.php" id="contact" method="post">
Name: <input type="text" name="name"/>
<br />Email: <input type="text" name="email"/>
<br />Message: <textarea name="message"></textarea>
<br /><input type="submit" value="Submit"/>
</form>

<script type="text/javascript">
window.onload = function() {
rememberFormInputs('contact', 'input-');
}

/**
* Set a cookie
* @param string cookie name
* @param string cookie value
* @param string cookie expiration counter in days
* @param string cookie path
* @param string cookie domain
* @param bool secure?
*/

function setCookie(name, value, expires, path, domain, secure) {
var today = new Date();
today.setTime(today.getTime());
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name+"="+escape(value) +
((expires) ? ";expires="+expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}

/**
* Get a cookie value
* @param string cookie name
*/

function getCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring(0, name.length))) {
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(";", len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}

/**
* Remebers form inputs after you fill them in
* @param string form id to remember fields
* @param string a prefix to prepend to all cookie names. (prevent naming conflicts)
*/

function rememberFormInputs(form_id, prefix) {
var form = document.getElementById(form_id);
var els = document.getElementsByTagName('input');
for (var i = 0; i < els.length; i++) {
var el = els.item(i);
if (el.type == 'text') {
el.onblur = function() {
var name = this.name;
var value = this.value;
setCookie(prefix + name, value);
};
var old_value = getCookie(prefix + el.name);
if (old_value && old_value != '') {
el.value = old_value;
}
}
}
}
</script>


The demo on the site I got this code from is a broken link. I would really appreciate any and all help to get this resolved. I know cookies are suppose to save even after closing out the browser.

Old Pedant
10-10-2011, 08:27 PM
That's because you did not set an expire time, so then the cookie becomes valid for the current session only. (In other words, without an expire time, coookies are kept in *memory* in the browser. With an expire time, they are saved as files on disk.)

You are doing


setCookie(prefix + name, value);

Notice that you *can* pass more info to the setCookie function:


function setCookie(name, value, expires, path, domain, secure) {

And read the embedded documentation:


* @param string cookie expiration counter in days

So, just change your call to


setCookie(prefix + name, value, 30); // expire in 30 days

Choose your own number of days.

nicky
10-10-2011, 09:05 PM
Yay! It worked! Thank you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum