/    Sign up×
Community /Pin to ProfileBookmark

saving form values to cookie: CGI vars in URL cause problems

Thanks to everyone who helped me here last week; I’ve studied a lot and the DOM syntax is no longer confusing. Now I have a problem with cookies ?

I’m trying to save the state of a form into a cookie so that the form is restored per user preferences when they return to the page.

The page is generated by a perl script, and this script uses CGI variables (i.e, “myscript.cgi?foo=2&boo=2”) to control some display options not related to the form date I am trying to save in a cookie.

Saving to a cookie works fine — as long as it is saved from the default URL (no CGI variables tacked on). i.e., from page “http://foo.com/sscript.cgi” user can click “Update Profile” and it saves the form data for selectors p1-p8. Form selectors p1-p8 are restored from the cookie when returning to “http://foo.com/sscript.cgi“. Just as intended/desired.

However, if user changes some other option (from another, different form in the same page), the URL may become “http://foo.com/sscript.cgi?foo=1“, then the cookie is (apparently) not read. I suspect the problem is with the “path” or (less likely) “domain” variabes in the cookie setting.

Is there some easy way to ignore cgi variables in the url when saving a cookie?

Just in case it’s a script bug, here’s the relevant functions:

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,”p1″, document.myprojects.p1, “select”);
getValue(string,”p2″, document.myprojects.p2, “select”);
getValue(string,”p3″, document.myprojects.p3, “select”);
getValue(string,”p4″, document.myprojects.p4, “select”);
getValue(string,”p5″, document.myprojects.p5, “select”);
getValue(string,”p6″, document.myprojects.p6, “select”);
getValue(string,”p7″, document.myprojects.p7, “select”);
getValue(string,”p8″, document.myprojects.p8, “select”);
for (var i=0;i<7+1;i++) {
getValue(string,”i”+i, eval(“document.myprojects.i”+i),”checkbox”);}
}

function replace(string,text,by) {
// Replaces text with by in string
var i = string.indexOf(text);
var newstr = ”;
if ((!i) || (i == -1)) {return string;}
newstr += string.substring(0,i) + by;
if (i+text.length < string.length) {
newstr += replace(string.substring(i+text.length,string.length),text,by);}
return newstr;
}

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 TYPE=”text/javascript” LANGUAGE=”JavaScript”><!–
var today = new Date(); var expires = new Date(today.getTime() + (356 * 86400000));
var searchString = replace(self.location.search.substring(1),”+”,” “);
if (searchString.length > 0) Set_Cookie(“userProfile”,searchString,expires);
//if (searchString.length > 0) Set_Cookie(“userProfile”,searchString,expires,’/’);
var userProfile = Get_Cookie(“userProfile”);

if (!userProfile) {
document.write(‘<P><B>No project preferences found for you.</b> Select from below and save.’);
document.write(‘If you have trouble, please allow cookies from foo.com.’);
} else {
document.write(‘<P><B>Found the following project preferences.</b> Make changes and ‘);
document.write(‘save if desired.’);
}

thanks in advance for any help!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@sneakyimpAug 04.2004 — ok...i'm no javascript heavy, but i believe that setting a cookie by default will attach the cookie to the current page and domain...i *think* you can specify a path as well for each cookie. you are probably going to have to do some parsing of the current url to remove the query string (i.e., everything after the "?").

setting a cookie arg is like this (this is from netscape's website):

[code=php]
Set-Cookie: name=value [;EXPIRES=dateValue] [;DOMAIN=domainName] [;PATH=pathName] [;SECURE]
[/code]


you'll want to set the PATH part explicitly. at least i think you will. hope this helps.
Copy linkTweet thisAlerts:
@randyestauthorAug 05.2004 — Thanks for the reply. I think you're right -- that the path is the key to my problem -- but I'm not positive. I've experimented with setting it to just '/' which whould make the cookie get sent for any page on the domain (which is correct and not overly-specific), but I still have problems whenever the CGI variables in the URL change.

I'll do some more research and experimentation. But I thinkn I will end up replacing (or enhancing) my setcookie/getcookie functions to also save and restore the state of any other forms (those that make the cgi vars in the url).

Any other ideas would be welcome. ?
×

Success!

Help @randyest spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...