Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    Cookies not saving after closing out of browser

    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/re...ubmission.html, and it only seems to work if you refresh the page, but not if you close out the browser and restart.

    Code:
    <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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    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
    Code:
        setCookie(prefix + name, value);
    Notice that you *can* pass more info to the setCookie function:
    Code:
    function setCookie(name, value, expires, path, domain, secure) {
    And read the embedded documentation:
    Code:
    * @param string cookie expiration counter in days
    So, just change your call to
    Code:
        setCookie(prefix + name, value, 30);  // expire in 30 days
    Choose your own number of days.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    nicky (10-10-2011)

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Yay! It worked! Thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •