SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting a Cookie

    I need to set a cookie using a sumbit button, i have the code and I just cant get it to run.

    Could you take a quick look at it and let me know if theer is a glaring problem.

    The button code:

    Code:
    <input type = "submit" value = "Submit" onclick = "setCookie()" />
    The cookie set code:

    Code:
    window.onload = usercookieset; //calls function usercookieset to either create cookie or check the value
                    
            function usercookieset(){ //sets the function
                var userName = "";//declares the variable with a null value
                if (document.cookie != ""){//tests the value of the cookie to see if its not set to null
                    userName = document.cookie.split ("=")[1];
                    
                document.getElementById("username").value = userName;//retrieves the value of the username text field and stores it in the userName variable               
                   }
            
            
            function setCookie(){ //function that is called to run the above function
                var expireDate = new Date();//gets the date and stores it as a variable
                expireDate.setMonth(expireDate.getMonth()+12);//sets the expiry time of the cookie
                
                var userName = document.getElementById("username").value;
    	        document.cookie = "userName=" + userName + ";expires=" + expireDate.toGMTString();
            }

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Morn View Post
    Code:
     expireDate.setMonth(expireDate.getMonth()+12);//sets the expiry time of the cookie
    This answer is waiting for you in another place. Setting the month value on it's own is incorrect.

    Code:
    expireDate.setDate( expireDate.getDate() + numberOfDays );
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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