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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts

    saved contents from an input box - can this be done with a textbox?

    This question is really a yes/no question.

    This piece of code performs a job which makes the contents of an input box available anywhere else in a website. It's done with cookies.

    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;
    }
    
    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');
    		}
    	}
    }
    Anyone who understand this code will know that calling the external file in the header and placing an appropriately named input box on another page will make visible the named variable saved elsewhere and stored as a cookie.

    ...for instance: <input type="text" name="r98"> will reveal the contents of the input box named "r98" on another page.

    It works brilliantly, and the magic is in the onload function.

    My question is can the same be achieved with a TEXTBOX instead of an input box? If so, how?
    Last edited by tpeck; 01-12-2007 at 12:59 PM.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Yes. Be sure to escape the textarea value and unescape on read else the line breaks will be lost. Also, be aware that cookies can only hold so many characters so, no lengthy essays please.

  • #3
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    Good, now I know it can be done, ...but how?

    My demo here shows what I would like to have happen, but I guess it means rewriting the onload function (with your other provisos).

    http://aapress.com.au/demo/input/page1.html

    My skills fall far short I'm afraid.

    Any help would be welcome. Thanks.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't look too closely but I did see for the textarea you are searching for inputs.

    var theInputs = document.getElementsByTagName('input');

    you need to change that to something like

    var theInputs = document.getElementsByTagName('textarea');

    to put it in your array. There may be other problems but that is one I saw at a quick glance.

    david_kw

  • #5
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    Thanks. I actually did try that before - it seemed the logical thing to do, but it doesn't do the trick.

    A textarea box must differ in some way to an input box - at least in this regard.

    The other thing, of course, is that I want to be able to do both an input box and a textarea box on the same page.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So you tried doing a second loop in your onload that might look something like this? (changed input to textarea and commented out the type='text' part)

    Code:
      var theInputs = document.getElementsByTagName('textarea');
      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');
        }
      }
    david_kw

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    OK - I tried doing what you said...

    Here is the demo: http://aapress.com.au/demo/input/page2.html

    Still it doesn't work.

    The code I tried was:

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

    I figured I had to change the variable names, right?
    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
    Yes I think you have to change variable names. It doesn't look like the loop is in your onload function though. Check the curly braces.

    david_kw

    Edit: By the way, I have to head out for the night. Good luck.

  • #9
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    Good grief! It works! Thanks again.

    (I've updated the demo.)

    Now all I have to do is figure out how to deal with the case where the textbox input contains line spaces - as rubenbuhr pointed out. ("Be sure to escape the textarea value and unescape on read else the line breaks will be lost.")

    Do you know what rubenbuhr meant - in terms of coding? I understand the need to do it but have no idea how.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #10
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    maybe this:
    http://www.barelyfitz.com/projects/encoder/
    will help?

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #11
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    I still have a problem with this textarea box.

    You can see from my demo that the input box and the textarea box are not saving independent values. But I've renamed the variables. Is it because they are using the same cookie? If so I am at a complete loss as to what to do.

    http://aapress.com.au/demo/input/page1.html

    Any ideas would be appreciated.
    Last edited by tpeck; 01-15-2007 at 12:46 AM.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #12
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    That looks neat. I work with a lot of cookies locally on my machine to keep track of music tracks I play, links I want to save without bookmarking, I have a textarea on my start page to write notes on, over 30 IDs to log into various web sites. I use a cookie to serve up a new ID everyday so I'm sure not to allow any of those IDs to lapse from lack of use at hotmail. So, I have a single globalCookie.js that use on different pages. Short of writing your pages for you, you can have a look at what I've done...(with help from others)

    PHP Code:
        var expDate = new Date();

        function 
    setCookie(isName,isValue,daysExpire){
            
    expDate = new Date();
        if(
    daysExpire){        expDate.setTime(expDate.getTime()+(86400000*daysExpire)); }
        else{
    expDate.setTime(expDate.getTime()+24*60*60*1000*365);}
            
    document.cookie isName+"="+escape(isValue)+";expires="+expDate.toGMTString();
        }

        function 
    getCookie(isName){
            var 
    cookieStr document.cookie;
            var 
    startSlice cookieStr.indexOf(isName+"=");

            if (
    startSlice == -1){return false}
            var 
    endSlice cookieStr.indexOf(";",startSlice+1);
            if (
    endSlice == -1){endSlice cookieStr.length}
            var 
    isData cookieStr.substring(startSlice,endSlice);
            var 
    isValue isData.substring(isData.indexOf("=")+1,isData.length);
            return 
    unescape(isValue);
        }

        function 
    dispCookie(isName){
            var 
    nValue getCookie(isName);
        return (
    nValue);


        }

        function 
    deleteCookie(isName){
       
            if (
    getCookie(isName)){document.cookie isName "="
                       
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";}
        } 

  • #13
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    OK. Thanks. I have attempted to incorporate your cookie functions into my (new) demo:

    http://aapress.com.au/demo/input2/page1.html

    I think I've altered all the names correctly, but it isn't saving anything, so I guess not.

    If I can get it to save, do you think it will distinguish the contents of the input boxes and the textarea boxes?
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #14
    Regular Coder
    Join Date
    Oct 2006
    Posts
    107
    Thanks
    3
    Thanked 5 Times in 5 Posts
    I am of no help anymore as I have never had a reason to fetch cookies created by a different page. You had something working, I hope you didn't trash what you had.

  • #15
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    Never mind - I want to use your cookie code - it includes the escape stuff.

    But I can't get it working yet.

    If I take it one step at a time i hope to get there.

    Basically, I need this demo to work and give separate displays for an input box and a textarea box.

    http://aapress.com.au/demo/input2/page1.html

    First, I need to get the cookie set using rubenbuhr's code. I've followed the same logic as the other code. Why doesn't it save anything?

    Can anyone see my stupid error?

    Code:
    function setCookie(isName,isValue,daysExpire){ 
            expDate = new Date(); 
        if(daysExpire){        expDate.setTime(expDate.getTime()+(86400000*daysExpire)); } 
        else{expDate.setTime(expDate.getTime()+24*60*60*1000*365);} 
            document.cookie = isName+"="+escape(isValue)+";expires="+expDate.toGMTString(); 
        } 
    
        function getCookie(isName){ 
            var cookieStr = document.cookie; 
            var startSlice = cookieStr.indexOf(isName+"="); 
    
            if (startSlice == -1){return false} 
            var endSlice = cookieStr.indexOf(";",startSlice+1); 
            if (endSlice == -1){endSlice = cookieStr.length} 
            var isData = cookieStr.substring(startSlice,endSlice); 
            var isValue = isData.substring(isData.indexOf("=")+1,isData.length); 
            return unescape(isValue); 
        } 
    
        function dispCookie(isName){ 
            var nValue = getCookie(isName); 
        return (nValue); 
    
    
        } 
    
        function deleteCookie(isName){ 
        
            if (getCookie(isName)){document.cookie = isName + "="+  
                       "; expires=Thu, 01-Jan-70 00:00:01 GMT";} 
        }  
     
        
    window.onload = function()
    {
    	var theInputs1 = document.getElementsByTagName('input');
    	for(var i = 0; i < theInputs1.length; i++)
    	{
    		if(theInputs1[i].type == 'text')
    		{
    			theInputs1[i].onkeyup = function()
    			{
    				setCookie(this.isName,this.isValue,9999);
    			}
    		}
    		if(getCookie(theInputs1[i].isName) != null)
    		{
    			theInputs1[i].isValue = getCookie(theInputs1[i].isName);
    		}
    	}
    	
    }

    If so, then I'll fix it, but I still have the input displays borrowing from each other. They must be separate. Well, I have hundreds of them, so if I can get two to work, the rest will follow!
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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