...

View Full Version : reading back a cookie entered into an input box



tpeck
02-02-2007, 03:50 AM
I have a cookie routine that works well. I want to check for the existence of a cookie value and do something if the cookie's value is "m".

The way the value of the cookie is input is into an input box:

<input class="Format4" maxlength="1" size="1" name="male">

Here is the cookie routine:


<!--
function createCookie(name,whatval,days)
{
if(whatval != '')
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"val="+whatval+expires+"; path=/";
}
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function readname()
{
var theInputs = document.getElementsByTagName('input');
for(var i = 0; i < theInputs.length; i++)
{
if(theInputs[i].type == 'text')
{
theInputs[i].onkeyup = function()
{
createCookie(this.name,this.value,9999);
}
}
if(readCookie(theInputs[i].name+'val') != null)
{
theInputs[i].value = readCookie(theInputs[i].name+'val');
}
}
}

//-->


And here is the HTML code:


<body onload="readcookie(male);">
<script>
document.writeln("<table border=\"0\" cellspacing=\"0\">");
document.writeln("<tr>");
document.writeln("<td valign=\"top\" nowrap>");
document.writeln("<p class=\"GoldText\">");
if (male =='m'){
document.writeln("<img border=\"0\" src=\"../img/listen5.png\" width=\"49\" height=\"52\">");
}
document.writeln("&nbsp;Hi&nbsp;<input name=\"visitor\" class=\"Format6\">");
document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
</script>

The bit in red is the problem. Given my cookie routine, how do I get it to read that cookie value and show the graphic if, and only if, the value is "m"?

Thanks.

david_kw
02-02-2007, 07:55 AM
A few things.

1) The readCookie() function returns the value of the cookie. You are calling it in your onload function but not setting any variable to it.

2) It isn't clear what value the variable 'male' holds from your code so I'm not positive but I suspect you meant to call it with the string "male" instead of the variable male.

3) Again the variable male is used to check to see if it holds the value "m" but it is never set in your code

So assuming that the string "male" is used when you are setting the cookie, you might try something like this



<body onload="male = readcookie('male');">
<script>
var male = null;
document.writeln("<table border=\"0\" cellspacing=\"0\">");
document.writeln("<tr>");
document.writeln("<td valign=\"top\" nowrap>");
document.writeln("<p class=\"GoldText\">");
if (male =='m'){
document.writeln("<img border=\"0\" src=\"../img/listen5.png\" width=\"49\" height=\"52\">");
}
document.writeln("&nbsp;Hi&nbsp;<input name=\"visitor\" class=\"Format6\">");
document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
</script>


I didn't test it but I think it is on the right track.

david_kw

glenngv
02-02-2007, 09:46 PM
<body onload="male = readcookie("male");">


<body onload="male = readcookie('male');">

david_kw
02-02-2007, 10:39 PM
Oooh, yeah. Good point!

Arty Effem
02-02-2007, 11:00 PM
<body onload="male = readcookie("male");">
<script>
var male = null;
document.writeln("<table border=\"0\" cellspacing=\"0\">");
document.writeln("<tr>");
document.writeln("<td valign=\"top\" nowrap>");
document.writeln("<p class=\"GoldText\">");
if (male =='m'){
document.writeln("<img border=\"0\" src=\"../img/listen5.png\" width=\"49\" height=\"52\">");
}

I didn't test it but I think it is on the right track.

If the cookie is read on load, the value returned to male won't be available when the image tag is written.


<script>
var male = readcookie("male");
.
.
.
if (male =='m'){
document.writeln("<img border=\"0\" src=\"../img/listen5.png\" width=\"49\" height=\"52\">");
}
.
.

david_kw
02-02-2007, 11:47 PM
heh, another good point!

tpeck
02-03-2007, 01:29 AM
Thanks everyone. I understand the reasoning behind it now.

The trouble is it does not work because of the way in which the cookie value is input as well as saved. There doesn't seem a way to get the value out of the cookie and do anything with it. I can display the value in an input box, but the method above does not seem to tranfer it to a variable.

My demo might help show this:

http://aapress.com.au/demo/cookie/name.html

The first block of document.writes displays the image correctly.

For the sake of demonstration, with the second block of document.writes, if the user enters 'm' for male, I'd like to prevent the image from displaying (well, once I can get that variable happening, I can do anything; change images etc.).



<html>

<head>
<title>Name</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<script>
<!--
function createCookie(name,whatval,days)
{
if(whatval != '')
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"val="+whatval+expires+"; path=/";
}
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function readname()
{
var theInputs = document.getElementsByTagName('input');
for(var i = 0; i < theInputs.length; i++)
{
if(theInputs[i].type == 'text')
{
theInputs[i].onkeyup = function()
{
createCookie(this.name,this.value,9999);
}
}
if(readCookie(theInputs[i].name+'val') != null)
{
theInputs[i].value = readCookie(theInputs[i].name+'val');
}
}
}

//-->
</script>
</head>

<body onload="readname();" bgcolor="#FFFFEE">

<p><b>Please enter your name:</b><br>
<br>
<input class="Format4" maxlength="20" size="15" name="visitor"> </p>
<table border="0" cellpadding="2" cellspacing="0" id="table1">
<tr>
<td>
<p><b>male</b></td>
<td>&nbsp;</td>
<td>
<p>
<input class="Format4" maxlength="1" size="1" name="male"> write ' m '</p>
</td>
</tr>
<tr>
<td>
<p><b>or female</b></td>
<td>&nbsp;</td>
<td>
<p>
<input class="Format4" maxlength="1" size="1" name="female"> write ' f '</p>
</td>
</tr>
<tr>
<td colspan="3" height="50">&nbsp;</td>
</tr>
</table>

<script>
document.writeln("<table border=\"0\" cellspacing=\"0\">");
document.writeln("<tr>");
document.writeln("<td valign=\"top\" nowrap>");
document.writeln("<p class=\"GoldText\">");
document.writeln("<img border=\"0\" src=\"listen5.png\" width=\"49\" height=\"52\">");
document.writeln("&nbsp;Hi&nbsp;<input name=\"visitor\" class=\"Format6\">");
document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
</script>

<script>
var male = readcookie("male");
document.writeln("<table border=\"0\" cellspacing=\"0\">");
document.writeln("<tr>");
document.writeln("<td valign=\"top\" nowrap>");
document.writeln("<p class=\"GoldText\">");
if (male=='m'){
document.writeln("<img border=\"0\" src=\"listen5.png\" width=\"49\" height=\"52\">");
}
document.writeln("&nbsp;Hi&nbsp;<input name=\"visitor\" class=\"Format6\">");
document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
</script>
</body>

</html>

david_kw
02-03-2007, 03:03 AM
You are getting an error that "readcookie" is not defined. It should be readCookie() in the call.

david_kw

tpeck
02-03-2007, 04:28 AM
Oops! All is now well.

Does anyone know of a way to get the info as to whether the user is male or female into that same variable by using two radio buttons?

It would need to remember the choice and display it the same way it remembers the 'm' or 'f'.

But I think it's more complex than it sounds.

Maybe something like this from Tony Davis' saverestore.js routine:


// RADIO BUTTON
//
if (fieldType == "radio") {
//alert(frm.elements[e].length);
for (x=0; x < frm.elements[e].length; x++) {
if (frm.elements[e][x].checked)
{
index = x
}
}
string = string + index + "\|";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum