By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,706 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,706 IT Pros & Developers. It's quick & easy.

Redirecting and Reading Cookie Problem

P: n/a
I'm supposed to develop a page that asks info as form values and when you hit
"submit" it takes you to a page that reads the values you entered into the
first page and displays those values in a message. I can't seem to find where
I'm going wrong. Here's the code:

Setting Cookie and Form Page:

<html><head><title>Problem3</title>
<script type="text/javascript" src="core.js"></script>
<script type="text/javascript">

var FormProblem=
{
init: function()
{
var the_button=document.getElementById("mybutton");

Core.addEventListener(the_button, "click", FormProblem.setCookie);
Core.addEventListener(the_button, "click", FormProblem.Redirect);

},

setCookie: function()
{

var name = document.getElementById("firstfield").value;
var age = document.getElementById("secondfield").value;
var color = document.getElementById("thirdfield").value;
var the_cookie = "username:name/userage:age/favcolor:color; path=..
/newpage.html;"
document.cookie = "my_cookie="+escape(the_cookie);
},

Redirect: function()
{
window.location.href="newpage.html"
}

};

Core.start(FormProblem)
</script></head>
<body>
<form id="youridentity">
<label>Your Name:</label>
<input type="text" name="yourname" id="firstfield"<br>
<label>Your Age:</label>
<input type="text" name="yourage" id="secondfield"<br>
<label>Your Favorite Color:</label>
<input type="text" name="favcolor" id="thirdfield"<br>
<input type="button" value="submit" id="mybutton">
</form>
</body>
</html>

newpage.html:

<html><head><title>NewPage</title>
<script type="text/javascript">

function readCookie (the_info)
{
if (document.cookie)
{
var the_cookie = document.cookie;
var the_cookie = unescape(the_cookie);
var broken_cookie = the_cookie.split("mycookie=");
var the_values = broken_cookie[1];
var broke_again = the_values.split("/");

var property_values="";

for (var loop=0; loop < broke_again.length; loop++)
{
var property_values = broke_again[loop];
var broken_info = property_values.split(":");
var the_property = broken_info[0];
var the_value = broken_info[1];
the_info[the_property] = the_value;
}
}

// Return the info you got passed
return the_info;

}

var cookie_info = {};
cookie_info = readCookie(cookie_info);

</script></head>
<body>

<script type = "text/javascript">

document.write("The was someone named"+cookie_info[username]+"at
age"+cookie_info[userage]+"who loved the color"+cookie_info[favcolor]);

</script>

</body>
</html>

Any idea of where I'm going wrong? I know it's gotta be something simple..
which'll just make me upset that I couldn't find it.

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200808/1

Aug 31 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Sorry about that:

Here's the update version of the setCookie() function:

setCookie: function()
{

var name = document.getElementById("firstfield").value;
var age = document.getElementById("secondfield").value;
var color = document.getElementById("thirdfield").value;
var the_cookie = "username:" + name + "/userage:" + age + "/favcolor:" +
color;
var the_cookie = escape(the_cookie);
var the_cookie = the_cookie + "path=/;";
document.cookie = "my_cookie="+the_cookie;
},

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200808/1

Aug 31 '08 #2

P: n/a
LayneMitch via WebmasterKB.com wrote:
I'm supposed to develop a page that asks info as form values and when you hit
"submit" it takes you to a page that reads the values you entered into the
first page and displays those values in a message. I can't seem to find where
I'm going wrong. Here's the code:
Might help if you describe what the problem is. All you've said is what
you hope to achieve, and that you haven't achieved it.
Aug 31 '08 #3

P: n/a
SAM
LayneMitch via WebmasterKB.com a écrit :
I'm supposed to develop a page that asks info as form values and when you hit
"submit" it takes you to a page that reads the values you entered into the
first page and displays those values in a message. I can't seem to find where
I'm going wrong. Here's the code:

Setting Cookie and Form Page:
<html><head><title>Problem3</title>
<script type="text/javascript">

var FormProblem=
{
init: function()
{
alert('init');
var the_button=document.getElementById("mybutton");
the_button.onclick = function() {FormProblem.setCookie();}
},

setCookie: function()
{
var name = document.getElementById("firstfield").value;
var age = document.getElementById("secondfield").value;
var color = document.getElementById("thirdfield").value;
var the_cookie = "username:"+name+
",userage:"+age+
",favcolor:"+color+
";path=/";
document.cookie = "my_cookie="+escape(the_cookie);
this.Redirect();
},

Redirect: function()
{
alert('redirect');
window.location.href="newpage.html"
}

};

window.onload = function() { FormProblem.init(); }

</script></head>
<body>
<form id="youridentity" onsubmit="return false;">
<label>Your Name:</label>
<input type="text" name="yourname" id="firstfield"<br>
<label>Your Age:</label>
<input type="text" name="yourage" id="secondfield"<br>
<label>Your Favorite Color:</label>
<input type="text" name="favcolor" id="thirdfield"<br>
<input type="button" value="submit" id="mybutton">
</form>
</body>
</html>
newpage.html:
<html><head><title>NewPage</title>
<script type="text/javascript">

if (document.cookie)
{
var the_cookie = document.cookie;
the_cookie = unescape(the_cookie);
var the_values = the_cookie.split("my_cookie=")[1];
the_values = the_values.split(";")[0];
the_values = the_values.split(',');

the_cookie = {};
for(var i=0, n=the_values.length; i<n; i++) {
var e = the_values[i].split(':');
the_cookie[e[0]] = e[1];
}
}

</script></head>
<body>

<script type = "text/javascript">

document.write("There was someone named: "+the_cookie.username+
"<br>at age: "+the_cookie.userage+
"<br>who loved the color: "+the_cookie.favcolor
);

</script>

</body>
</html>

--
sm
Aug 31 '08 #4

P: n/a
SAM wrote:

Thank you for responding. I'm breaking down your code and this is what I've
come up with:
setCookie: function()
{
var name = document.getElementById("firstfield").value;
var age = document.getElementById("secondfield").value;
var color = document.getElementById("thirdfield").value;
var the_cookie = "username:"+name+
",userage:"+age+
",favcolor:"+color+
";path=/";
document.cookie = "my_cookie="+escape(the_cookie);
this.Redirect();
},
Okay, so far the difference in my code and yours is that I'm using event
listeners and you aren't. This section is different because of the use of
";path=/";
So I incorporated that.
>
window.onload = function() { FormProblem.init(); }

</script></head>
<body>
<form id="youridentity" onsubmit="return false;">
<label>Your Name:</label>
<input type="text" name="yourname" id="firstfield"<br>
<label>Your Age:</label>
<input type="text" name="yourage" id="secondfield"<br>
<label>Your Favorite Color:</label>
<input type="text" name="favcolor" id="thirdfield"<br>
<input type="button" value="submit" id="mybutton">
</form>
</body>
</html>
Nothing really different here. I'm using Firebug (which I haven't fully
figured how to use yet..I'm new), and it's saying that the cookies are being
set once newpage.html is loaded. It's just not reading the cookie for
whatever the reason is. So..moving on...
>newpage.html:

<html><head><title>NewPage</title>
<script type="text/javascript">

if (document.cookie)
{
var the_cookie = document.cookie;
the_cookie = unescape(the_cookie);
var the_values = the_cookie.split("my_cookie=")[1];
the_values = the_values.split(";")[0];
the_values = the_values.split(',');

the_cookie = {};
for(var i=0, n=the_values.length; i<n; i++) {
var e = the_values[i].split(':');
the_cookie[e[0]] = e[1];
}
}

</script></head>
The only difference here is that because you used ";path=/"; I now have to
split by a ";".
><body>

<script type = "text/javascript">

document.write("There was someone named: "+the_cookie.username+
"<br>at age: "+the_cookie.userage+
"<br>who loved the color: "+the_cookie.favcolor
);

</script>
Okay...this is where the problem lies. I'm referencing the value of the
cookies by using strings in cookie_info[username], and the browser isn't
displaying anything. I just tried using your 'object.method' example and the
browser displayed the sentence, but each value was 'undefined'...why is that?

There shouldn't be any reason for that. I just had the same issue a week ago
with cookies and the solution was that if I 'return' the array of info into
an object that calls 'readCookie()' - which I did in

var the_property = broken_info[0];
var the_value = broken_info[1];
the_info[the_property] = the_value;
}
}

// Return the info you got passed
return the_info;
I should be able to reference the value by 'object[string]= value'. What's
the problem with this solution know?

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200808/1

Aug 31 '08 #5

P: n/a
Sorry about that. Hopefully you can follow this last post.

Firebug got in the way and screwed up my message so your parts of the code I
was responding to somehow got deleted.

--
Message posted via http://www.webmasterkb.com

Aug 31 '08 #6

P: n/a
SAM
LayneMitch via WebmasterKB.com a écrit :
SAM wrote:

Thank you for responding. I'm breaking down your code and this is what I've
come up with:
> setCookie: function()
{
var name = document.getElementById("firstfield").value;
var age = document.getElementById("secondfield").value;
var color = document.getElementById("thirdfield").value;
var the_cookie = "username:"+name+
",userage:"+age+
",favcolor:"+color+
";path=/";
document.cookie = "my_cookie="+escape(the_cookie);
this.Redirect();
},

Okay, so far the difference in my code and yours is that I'm using event
listeners and you aren't.
I don't because I Haven't 'core.js' :-)

This section is different because of the use of ";path=/";
It seam to me you did too, no ?

I use ',' as separator in the_cookie's value
So I incorporated that.
>window.onload = function() { FormProblem.init(); }
If you did keep your core-functions, not sure that could work.
>>
</script></head>
<body>
<form id="youridentity" onsubmit="return false;">
<label>Your Name:</label>
<input type="text" name="yourname" id="firstfield"<br>
<label>Your Age:</label>
<input type="text" name="yourage" id="secondfield"<br>
<label>Your Favorite Color:</label>
<input type="text" name="favcolor" id="thirdfield"<br>
<input type="button" value="submit" id="mybutton">
</form>
</body>
</html>

Nothing really different here.
Yes it's copy-paste.
I'm using Firebug (which I haven't fully
figured how to use yet..I'm new), and it's saying that the cookies are being
set once newpage.html is loaded. It's just not reading the cookie for
whatever the reason is. So..moving on...
If cookies exist they are surely read.
>>newpage.html:
<html><head><title>NewPage</title>
<script type="text/javascript">

if (document.cookie)
{
var the_cookie = document.cookie;
the_cookie = unescape(the_cookie);
var the_values = the_cookie.split("my_cookie=")[1];
the_values = the_values.split(";")[0];
the_values = the_values.split(',');
Notice here how I set the object from the_values
(instead giving that to something I don't understand)
> the_cookie = {};
for(var i=0, n=the_values.length; i<n; i++) {
var e = the_values[i].split(':');
the_cookie[e[0]] = e[1];
}
}

</script></head>

The only difference here is that because you used ";path=/"; I now have to
split by a ";".
then I split by ',' and
and I fill-up the object from this array 'the_values'
the_cookie[aNewName] = aNewValue;
><body>

<script type = "text/javascript">

document.write("There was someone named: "+the_cookie.username+
"<br>at age: "+the_cookie.userage+
"<br>who loved the color: "+the_cookie.favcolor
);

</script>

Okay...this is where the problem lies. I'm referencing the value of the
cookies by using strings in cookie_info[username], and the browser isn't
displaying anything.
document.write("The was someone named: "+the_cookie['username']);
works fine in my browser.
I just tried using your 'object.method' example and the
browser displayed the sentence, but each value was 'undefined'...why is that?
If you have not added ';path/' to the cookie, I think you have to do :

function readCookie ()
{
if (document.cookie)
{
var the_cookie = document.cookie;
the_cookie = unescape(the_cookie);
the_cookie = the_cookie.split("=")[1]; // split by '='
var the_values = the_cookie.split("/");

var the_info = {}; // here we build a new object

for (var loop=0; loop < the_values.length; loop++)
{
var property_values = the_values[loop].split(":");
var the_property = property_values[0];
var the_value = property_values[1];
the_info[the_property] = the_value;
}

// Return the info you got passed
return the_info;
}
}

var cookie_info = readCookie();
--
sm
Aug 31 '08 #7

P: n/a
SAM wrote:
document.write("The was someone named: "+the_cookie['username']);
works fine in my browser.

Got it. This was a syntax error. I didn't have the single quotations
before/after variables.
>If you have not added ';path/' to the cookie, I think you have to do :

function readCookie ()
{
if (document.cookie)
{
var the_cookie = document.cookie;
the_cookie = unescape(the_cookie);
the_cookie = the_cookie.split("=")[1]; // split by '='
Got it. This was another one of my errors.
var the_values = the_cookie.split("/");

var the_info = {}; // here we build a new object

for (var loop=0; loop < the_values.length; loop++)
{
var property_values = the_values[loop].split(":");
var the_property = property_values[0];
var the_value = property_values[1];
the_info[the_property] = the_value;
}

// Return the info you got passed
return the_info;
}
}

var cookie_info = readCookie();
Thanks for your expertise. The problem has been solved. I basically kept my
code and saw that I needed the quotations for the cookie_name['username']...
etc. Also I did use ;path=/ originally, but I found a better solution by
using "path=newpage.html;". Once I made these changes plus the one
immediately above, everything worked.

I do like your code because it's showing me a different view without the file,
which in turn is showing me a more abstract view about the use of objects and
methods(something different than textbook versions). :-)

Thanks.

--
Message posted via http://www.webmasterkb.com

Aug 31 '08 #8

P: n/a
SAM wrote:
LayneMitch via WebmasterKB.com a crit :
>...
I'm using Firebug (which I haven't fully figured how to use yet..I'm
new), and it's saying that the cookies are being set once newpage.html
is loaded. It's just not reading the cookie for whatever the reason is.

If cookies exist they are surely read.
Sorry for the nitpick, but there are actually various reasons why a
cookie could not be read. They could be disabled, or set as limitedly
accessible by the browser. A cookie can also be expired (or non-
persistent), and domain name and/or path restrictions could apply.

I think the following is an excellent article:
http://en.wikipedia.org/wiki/HTTP_cookie

--
Bart
Sep 1 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.