Click to See Complete Forum and Search --> : Cannot set multiple cookies


therealphil
09-09-2003, 05:58 AM
Hi
I have a piece of Javascript that validates data entered into an HTML form. At the end of my form are three lines of code. Each one calls a function, called setCookie, which sets a cookie containing the values of some of the form fields.

The problem is that when I run the script, only the last cookie actually gets set, as if each of the three cookies overwrites the cookie before it.

I thought that my field values weren't being properly assigned to the cookie values, but I modified my code to only set one cookie, and I did this with each of my three cookies and they worked fine. If I try to set more than one cookie, I always end up with only the last cookie being set.

Here are my three lines of code:

setCookie("vecProducts",products[0] +','+ products[1] +','+ products[2],";path=/;","");
setCookie("vecDownloadorCD",Download +','+ CD +','+ DownloadCD,";path=/;","");
setCookie("vecModules",document.EvalForm.webother.value,";path=/;","");


This is the 'setCookie' function:

// setCookie function
function setCookie(cookieName, cookieValue, cookiePath, cookieExpires){
cookieValue = escape(cookieValue);
document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + cookiePath;
}


Here is the page the code is on:
http://www.vector-networks.com/test/products/download.htm

Thanks, Phil

Khalid Ali
09-09-2003, 06:13 AM
sounds like you have same name for all cookies,make sure they have unique names.

therealphil
09-09-2003, 06:49 AM
They do have different names:

setCookie("vecProducts",pro...
setCookie("vecDownloadorCD",Dow...
setCookie("vecModules",doc...

The first is called "vecProducts", the second, "vecDownloadorCD" and the third, "vecModules".

If I run them individually, i.e. change my code to:


setCookie("vecProducts",products[0] +','+ products[1] +','+ products[2],";path=/;","");

It works fine, and the same with:


setCookie("vecDownloadorCD",Download +','+ CD +','+ DownloadCD,";path=/;","");

and


setCookie("vecModules",document.EvalForm.webother.value,";path=/;","");

The code seems absolutely fine, but IE isn't setting the cookies.

Khalid Ali
09-09-2003, 07:07 AM
interesting,because it seems like you have roblem with your expire time

I have tested your code with only one change in it and it works on both browsers

var expire = new Date ();
expire.setTime (expire.getTime() + (60000)); //1 mins from now!
cookieValue = escape(cookieValue);
document.cookie = cookieName + "=" + cookieValue + ";expires=" + expire.toGMTString() + cookiePath;

therealphil
09-09-2003, 07:22 AM
Thanks!!!!!

Just my luck it'd be something simple like that. Well thats a day and a half wasted.

Cheers, Phil

Khalid Ali
09-09-2003, 07:27 AM
You are welcome...
not wasted buddy,thats how programming works...somedays are wayyyyyyyyyy longer then the others..lol

therealphil
09-09-2003, 07:43 AM
ummmmmmmm................. I have another problem.

My redirect page has a small bit of script which writes the value of the cookies to the page (this is just to test them) but they all come back as 'undefined'. I have looked at the actually txt file on my PC storing the cookies, and the values are defined. This is the code that writes to stuff to the page:

var vecProducts = getCookieValue("vecProducts");
document.write("vecProducts = "+ vecProducts +"<br>");

var vecDownloadorCD = getCookieValue("vecDownloadorCD");
document.write("vecDownloadorCD = "+ vecDownloadorCD +"<br>");

var vecModules = getCookieValue("vecModules");
document.write("vecModules = "+ vecModules +"<br>");

And this is the function that actually cuts the cookie up and gets the value:

function getCookieValue(cookieName){
var cookieValue=document.cookie;
var cookieStartsAt=cookieValue.indexOf(" " + cookieName + "=");
if (cookieStartsAt==-1){
cookieStartsAt=cookieValue.indexOf(cookieName + "=");}
if (cookieStartsAt==-1){cookieValue=null;}
else {
cookieStartsAt=cookieValue.indexOf("=", cookieStartsAt) +1;
var cookieEndsAt=cookieValue.indexOf(";", cookieStartsAt);
if (cookieEndsAt==-1){cookieEndsAt=cookieValue.length;}
cookieValue=unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
}

Sorry for being a pain but..... Help?!

Khalid Ali
09-09-2003, 10:33 AM
use the following function,it will return the coockie value, you pass the cookie name to it as parameter

/*
@param CookieName is passed to get Cookie method to read a cookie
@return returns an array of values in the cookie separated by a ` character.
*/
function getCookie (CookieName) {
var cname = CookieName + "=";
var i = 0;
while (i < document.cookie.length) {
var j = i + cname.length;
if (document.cookie.substring(i, j) == cname){
var leng = document.cookie.indexOf (";", j);
if (leng == -1) {
leng = document.cookie.length;
}
return (unescape(document.cookie.substring(j, leng))).split("`");
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
}