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
    New Coder
    Join Date
    Oct 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Cookies help... If I can't do this I'm doomed! This is for my site...

    Hi,
    I write this script:
    Code:
     <FORM name="frm1">
     <input id="Text1" name="txt1" style="width: 65px" type="text" onchange='document.sector1="Your folder name is: "+document.frm1.txt1.value+". Personalize this webpage for your needs! Cookies must be enabled."' onBlur='alert(document.sector1)' maxlength="10"/>
     </FORM>
    I want to make it so the cookie gets saved to the user's computer, and even if the user goes to a new page and returns, the text box value is the same as the user entered. Is this possible?

    Thanks a million!

    Striker
    Last edited by Striker14537; 10-20-2006 at 11:12 PM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Its setup to work with any number of text inputs.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
    <script type="text/javascript">
    function createCookie(name,whatval,days)
    {
    	if(value != '')
    	{
    		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="+whatvalue+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;
    }
    window.onload = function()
    {
    	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>
    <form action="#">
    <input type="text" name="txt1">
    <input type="text" name="txt2">
    <input type="text" name="txt3">
    </form>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Oct 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It doesn't work for me Help?

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts
    You have 3 different names for 1 variable
    Change:
    PHP Code:
    function createCookie(name,whatval,days)
    {
        if(
    value != '')
        {
            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="+whatvalue+expires+"; path=/";
        }

    Worked for me when I used 'whatvalue' instead of 'whatval' and 'value' for consistancy

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    My fault. As stated I mixed up variable names. This should work
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
    <script type="text/javascript">
    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;
    }
    window.onload = function()
    {
    	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>
    <form action="#">
    <input type="text" name="txt1">
    <input type="text" name="txt2">
    <input type="text" name="txt3">
    </form>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    New Coder
    Join Date
    Oct 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works! You guys are the best! Thank you!

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    Aerospace, this seems to work for my cookie requirement too (interesting how it doesn't require a submit!)

    But there seem to be two limitations:

    1) it works in FF but sets up a completely separate cookie for this browser - is this normal? Does FF read different cookies?
    2) any <body onload="whatever()"> don't work any more.

    If these two problems could be overcome, I could dispense with masses amounts of coding on my 1000 pages!
    Last edited by tpeck; 10-21-2006 at 04:28 AM.

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    1) The cookie will be set according to how the browser works, don't worry about this part.
    2) You can only have one onload on a page. So if you have one in your body get rid of it and move the function call in with the previous window.onload after the for loop.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    OK - I guess I will have to live with the browser differences with regard to cookies, but I have learnt something: you can set up a site to work a particular way saving any number of cookies, then switch browsers and it's as if your cookies never existed! That seems a fairly daunting prospect if, as I am doing, one is setting up a very large rating system. Perhaps I will have to warn the user not to switch browsers half way through working with the site.

    The onload advice worked perfectly. Thanks. In fact, the whole thing works beautifully - I wish I could show you. The ratings I am gathering display like magic without even pressing enter, and even without a form on other pages that tabulate and display the entire matrix. It's like a magic trick!


  •  

    Posting Permissions

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