The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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();
}