Hi
This is an old question but I never received an answer that worked for my situation, or I just didn't understand the answer correctly. I am very new at javascript and I am attempting to write a cookie for a form that includes a visitor's name, address, phone number, email address so that each time the visitor returns to the form, this information is automatically filled out with the information left from before.
I tried this code once and it worked, but then I changed the expiration and it never worked again.
I believe that there is something wrong with the Get_Cookie(Name) and the Set_Cookie parts but I am very new to this and I haven't received an explanation as to why it's wrong and how to fix it
Here's the code:
<SCRIPT LANGUAGE="JavaScript"><!--
//"Cookie form values storage"
function Get_Cookie(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));
}
function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "" +
( (path) ? ";path=" + path : "" +
( (domain) ? ";domain=" + domain : "" +
( (secure) ? ";secure" : ""
}
function setupForm() {
if (userProfile) getValues(userProfile);
}
function getValues(string) {
getValue(string,"FirstLastName", document.ptsregb.FirstLastName, "text"
getValue(string,"EmailAddress", document.ptsregb.EmailAddress, "text"
getValue(string,"Program_Title", document.ptsregb.Program_Title, "select"
getValue(string,"Location", document.ptsregb.Location, "text"
getValue(string,"PhoneNumber", document.ptsregb.PhoneNumber, "text"
for (var i=0;i<7+1;i++)
getValue(string,"i"+i, eval("document.ptsregb.i"+i), "checkbox"
}
function onCheck(string) { if (string == "on" return true; return false; }
function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType
var startPos = string.indexOf(elementName + "="
if (startPos > -1) {
startPos = startPos + elementName.length + 1;
var endPos = string.indexOf("&",startPos);
if (endPos == -1) endPos = string.length;
var elementValue = unescape(string.substring(startPos,endPos));
if (elementType == "text" object.value = elementValue;
if (elementType == "password" object.value = elementValue;
if (elementType == "select" object.selectedIndex = elementValue;
if (elementType == "checkbox" object.checked = onCheck(elementValue);
if (elementType == "radio" object[elementValue].checked = true;
}
}
//--></SCRIPT>
<title></title>
</HEAD>
<BODY onLoad="setupForm()" background="lt%20background.gif" bgproperties="fixed">
<SCRIPT LANGUAGE="JavaScript"><!--
//Establishes the Cookie Expiration Date
var today = new Date();
var expires = new Date(today.getTime() + (365 * 24 * 60 * 60 * 1000)); // expires one year from now
var userProfile = Get_Cookie("userProfile"
if (!userProfile) {
document.write('<P>Welcome,<P>According to your records ');
document.write('you have not set your user profile:');
}
else {
document.write('<P>Welcome back,<P>According to your records ');
document.write('the following settings are held in your profile:');
}
//--></SCRIPT>
Please help! This problem has and continues to plague me.
Thanks! [sig][/sig]
This is an old question but I never received an answer that worked for my situation, or I just didn't understand the answer correctly. I am very new at javascript and I am attempting to write a cookie for a form that includes a visitor's name, address, phone number, email address so that each time the visitor returns to the form, this information is automatically filled out with the information left from before.
I tried this code once and it worked, but then I changed the expiration and it never worked again.
I believe that there is something wrong with the Get_Cookie(Name) and the Set_Cookie parts but I am very new to this and I haven't received an explanation as to why it's wrong and how to fix it
Here's the code:
<SCRIPT LANGUAGE="JavaScript"><!--
//"Cookie form values storage"
function Get_Cookie(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));
}
function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "" +
( (path) ? ";path=" + path : "" +
( (domain) ? ";domain=" + domain : "" +
( (secure) ? ";secure" : ""
}
function setupForm() {
if (userProfile) getValues(userProfile);
}
function getValues(string) {
getValue(string,"FirstLastName", document.ptsregb.FirstLastName, "text"
getValue(string,"EmailAddress", document.ptsregb.EmailAddress, "text"
getValue(string,"Program_Title", document.ptsregb.Program_Title, "select"
getValue(string,"Location", document.ptsregb.Location, "text"
getValue(string,"PhoneNumber", document.ptsregb.PhoneNumber, "text"
for (var i=0;i<7+1;i++)
getValue(string,"i"+i, eval("document.ptsregb.i"+i), "checkbox"
}
function onCheck(string) { if (string == "on" return true; return false; }
function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType
var startPos = string.indexOf(elementName + "="
if (startPos > -1) {
startPos = startPos + elementName.length + 1;
var endPos = string.indexOf("&",startPos);
if (endPos == -1) endPos = string.length;
var elementValue = unescape(string.substring(startPos,endPos));
if (elementType == "text" object.value = elementValue;
if (elementType == "password" object.value = elementValue;
if (elementType == "select" object.selectedIndex = elementValue;
if (elementType == "checkbox" object.checked = onCheck(elementValue);
if (elementType == "radio" object[elementValue].checked = true;
}
}
//--></SCRIPT>
<title></title>
</HEAD>
<BODY onLoad="setupForm()" background="lt%20background.gif" bgproperties="fixed">
<SCRIPT LANGUAGE="JavaScript"><!--
//Establishes the Cookie Expiration Date
var today = new Date();
var expires = new Date(today.getTime() + (365 * 24 * 60 * 60 * 1000)); // expires one year from now
var userProfile = Get_Cookie("userProfile"
if (!userProfile) {
document.write('<P>Welcome,<P>According to your records ');
document.write('you have not set your user profile:');
}
else {
document.write('<P>Welcome back,<P>According to your records ');
document.write('the following settings are held in your profile:');
}
//--></SCRIPT>
Please help! This problem has and continues to plague me.
Thanks! [sig][/sig]