473,378 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Redirecting and Reading Cookie Problem

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
8 2036
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: AVance | last post by:
Hi, I've come across this scenario in ASP.NET 1.1 with forms authentication where the forms auth doesn't seem to timeout correctly, nor redirect to the login page. I have done some testing, and...
1
by: hecsan07 | last post by:
I am a novice programmer. I want to give my site the ability to remember users. I want to do this the easiest possible way--using cookies to store username and password--since I am not concerned...
9
by: Mike Reed | last post by:
I must be having a "senile" day! I cannot recall, nor get to work, code to read a cookie's expiration date/time in an ASP page/VBScript. What am I missing? *** Sent via Developersdex...
29
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
41
by: amygdala | last post by:
Hello all, I have posted a similar question in comp.lang.php in the past, but haven't had any response to it then. I kinda swept the problem under the rug since then. But I would really like to...
9
by: Jonathan Wood | last post by:
I've spent days trying to come up with a solution. I'd appreciate it if anyone can help. My site requires all users to log on. There are three different roles of users, and each user type will...
3
by: jacusp | last post by:
Hi, I have problem with reading cookies. I'd like to save fields of my form into cookies and use them by another page. I save cookies: foreach ($_POST as $idx =$value) { setcookie($idx,...
0
by: Anup Daware | last post by:
Hi All, I am facing this very weird issue, for every server request my control is redirecting to the login page and again coming back to the actual page from where the request was initiated when I...
4
by: | last post by:
Hello, I have a couple of web apps built using C# in VS2008 (.NET 2.0) and in some circumstances they exhibit an anoying feature. If I start in appA and follow a link to appB I can still see the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.