SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: saving a form value to a cookie
-
Apr 16, 2001, 22:35 #1
- Join Date
- Feb 2001
- Posts
- 134
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I want to save the form value for a persons USERNAME in a cookie when they are logging into my free e-mail service. So when they come back I can lookup the cookie and write the value for their username back into that spot in the form.
I've got it working for the drop-down menu so that it will change to whatever domain they selected the last time they logged in but I can't get it to work for the username box.
in case you want to see the form I'm talking about it's the e-mail login form on http://www.guavamail.com
Thanks.Guava
-
Apr 17, 2001, 07:22 #2
- Join Date
- Mar 2001
- Location
- London, UK
- Posts
- 43
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Man this question keeps coming up!
You can use a function like this to store the username:
<script language="JavaScript">
// makeCookie() function
// Coded by Rips (April 2001)
function makeCookie() {
// Declare a date object then set its month property to 3 months from now
var expireAt = new Date;
expireAt.setMonth(expireAt.getMonth() + 3);
// Store a cookie containing the username and expiry date
document.cookie = "name=" + escape(document.info.user.value) + "; expires=" + expireAt.toGMTString();
}
</script>
Then add an action to your form, say onSubmit or onClick that will call the function when you need to store the username:
<form onSubmit="makeCookie()">
On other pages you can write the value to a form field using sommat like this:
<script language="JavaScript">
// fillIn() function
// Coded by Rips (March 2001)
function fillIn() {
// Read the cookie property. This returns all cookies for this document
var allCookies = document.cookie;
// Look for the start of the cookie named "name"
var pos = allCookies.indexOf("name=");
// If we find a cookie by that name, extract and use its value
if (pos != -1) {
// Start of cookie value
var start = pos + 5;
// End of cookie value
var end = allCookies.indexOf(";", start);
if (end == -1) end = allCookies.length;
// Extract the value
var cookieCrumb = allCookies.substring(start, end);
// Decode it
cookieCrumb = unescape(cookieCrumb);
// Now that we have the cookie value we can use it
document.info.user.value = cookieCrumb;
}
else
document.info.user.value = "username not set";
}
</script>
Then just call this function from the body tag with:
<body onLoad="fillIn()">
Is that what you're looking for geezer?
-
Apr 17, 2001, 10:58 #3
- Join Date
- Feb 2001
- Posts
- 134
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Worked perfectly. Thanks a lot!
Guava
Bookmarks