Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    875
    Thanks
    53
    Thanked 6 Times in 5 Posts

    reading back a cookie entered into an input box

    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:

    Code:
    <!--
    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:

    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.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
    <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
    Last edited by david_kw; 02-02-2007 at 10:37 PM. Reason: fixed quote problem pointed out by glenngv

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts

    Use single quotes in the js parameter

    Quote Originally Posted by david_kw View Post
    Code:
    <body onload="male = readcookie("male");">
    <body onload="male = readcookie('male');">
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oooh, yeah. Good point!

  • #5
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by david_kw View Post
    Code:
    <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.

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

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    heh, another good point!

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    875
    Thanks
    53
    Thanked 6 Times in 5 Posts
    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.).


    Code:
    <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>
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #8
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are getting an error that "readcookie" is not defined. It should be readCookie() in the call.

    david_kw

  • #9
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    875
    Thanks
    53
    Thanked 6 Times in 5 Posts
    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:

    Code:
    // 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 + "\|";
    			}
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •