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 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Storing Information form data in a cookie

    i'm trying to make it so that after validation if the data has been previously saved it should be retrieved from the cookie and displayed on the page . Currently it doesn't seem as if the cookie is saving anything.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    
    
    
    
    <head>
    <script type="text/javascript" src="fadeslideshow.js">
    </script>
    
    	  <link href="stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" />
      <link href="stylesheets/print.css" media="print" rel="stylesheet" type="text/css" />
      <!--[if IE]>
          <link href="stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" />
      <![endif]-->
    <link href='http://fonts.googleapis.com/css?family=Italianno' rel='stylesheet' type='text/css'>	
    
    
    </head>
    
    
    <script type="text/javascript">
    window.onload= text; 
    
    var IdAry=['status1'];
    
    
    </script>
    
    
    //required format 12-34-567
    
    
    
    <body onunload="saveSelections(document.forms[0])">
    	<div id="header"> <h1> Rose Photgraphy</h1> </div>
    	<div class="nav">
    <ul>
    <li><a href="index.htm" >Home</a></li>
    <li><a href="gallery.htm">Gallery</a></li>
    <li><a href="404.htm" >Contact</a></li>
    <li><a href="404.htm">XML</a></li>
    </ul>
    </div>
    <form>
    <input type="text" name="txt" onBlur="this.value=(this.value.match(/^\D?(\d{2})\D?\D?(\d{2})\D?(\d{3})$/))?this.value.substring(0,2)+'-'+this.value.substring(2,4)+'-'+this.value.substring(4,7):this.value">
    </FORM><SCRIPT language=JavaScript type="">loadSelections(document.forms[0]);</SCRIPT>
    
    	<div class='border-radius-test' id='border-radius'>
      <p id="status1">
    <span id="span1">this is only a test</span>
    <span class="hide">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eu sapien eget justo porta posuere sollicitudin sed orci. Nunc dictum gravida elit, et sodales velit euismod sed. Nulla facilisi. Cras venenatis, libero ut varius adipiscing, mauris diam tristique eros, quis scelerisque justo enim pulvinar enim. Nulla ante mi, dictum et aliquam ut, interdum at leo. Ut imperdiet, mauris vel tempus euismod, purus nibh gravida urna, eget mattis ante mauris a libero. Maecenas malesuada sagittis justo et gravida. Pellentesque porttitor sollicitudin eleifend. Nulla facilisi. Maecenas feugiat laoreet orci, mattis tempus sem fermentum id. Aenean pretium blandit magna, ac ultrices turpis gravida eget. Proin at bibendum nisl. Phasellus aliquam, enim eu semper adipiscing, turpis lacus tristique nulla, tempor auctor magna odio in justo. Vestibulum vitae purus mauris. Morbi ornare enim sit amet ipsum volutpat a luctus metus condimentum. Quisque aliquet nunc et massa aliquam ut pulvinar justo gravida.
    
    Sed sit amet elit nisl, vitae elementum magna. Phasellus metus dolor, scelerisque eu vulputate sit amet, lobortis et ante. Donec ac ultricies purus. Vivamus eget arcu sapien. Maecenas a orci at nulla luctus ullamcorper. Nulla scelerisque laoreet mollis. Proin sit amet enim ut mauris imperdiet aliquam eget vitae nulla. Duis non enim velit. Phasellus nisl lacus, malesuada eget luctus non, faucibus in mauris. Nunc condimentum, est eget luctus luctus, erat felis malesuada sem, in placerat tellus est vel arcu. Donec ligula erat, rhoncus ut cursus sed, volutpat a urna. Sed ultricies bibendum neque, a blandit velit varius ac. Duis orci nibh, tempor ac laoreet nec, aliquam eu turpis.
    
    Pellentesque a orci arcu. Duis eget diam ac nulla pellentesque egestas. Suspendisse a nunc turpis. Cras dignissim aliquam dui, eu consequat mi venenatis at. Mauris non tellus a purus sollicitudin lobortis sit amet vitae velit. Nulla sit amet sodales ipsum. Nam leo dui, imperdiet et pharetra.</span>
    </p>
    
    
    </div>
    
    
    
    
    </body>
    
    </html>

    This is what is in the javascript file

    Code:
    getCookie:function(Name){ 
    		var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    		if (document.cookie.match(re)) //if cookie found
    			return document.cookie.match(re)[0].split("=")[1] //return its value
    		return null
    	},
    
    	setCookie:function(name, value){
    		document.cookie = name+"=" + value + ";path=/"
    	}
    }
    
    function saveSelections(frm) {
    		var setvalue;
    		var fieldType;
    		var index;
    		var formname = frm.name;
    
    		// Expire cookie in 999 days.
    		var today = new Date();
    		var exp   = new Date(today.getTime()+999*24*60*60*1000);
    
    		var string = "formname=" + formname + "|";
    		var cookieName = formname;
    
    		//alert(exp);
    		//alert(formname);
    
    		var n = frm.length;
    		for (i = 0; i < n; i++)
    
    		{
    			e 	    = frm[i].name;
    			fieldValue  = frm[i].value;
    			fieldType   = frm[i].type;
    
    			//alert(e);
    			//alert(fieldType);
    			//alert(fieldValue);
    
    	
    
    			//
    			// TEXT, TEXTAREA, and DROPDOWN
    			//
    			if ((fieldType == "text") ||
    			    (fieldType == "textarea") ||
    			    (fieldType == "select-one"))
    			{
    		    	string = string + frm.elements[e].value + "\|";
    		    	//alert("text");
    			}
    
    			
    			// HIDDEN field
    			//
    			if (fieldType == "hidden")
    			{
    		    	string = string + frm.elements[e].value + "\|";
    		    	//alert("text");
    			}
    		}
    
    //alert(string);
    setCookie(cookieName, string, exp); }
    
    //
    // LOAD FORM FIELD SELECTIONS FROM SAVED COOKIES
    //
    
    function loadSelections(frm) {
    var e;
    var z;
    var x;
    var cookieName;
    var fieldArray;
    var fieldValues;
    var fieldValue;
    
    var formname = frm.id;
    
    // Retrieve form elements from cookie and split into array.
    
    cookieName  = formname;
    fieldValues = getCookie(cookieName);
    fieldArray  = fieldValues.split("\|");
    
    //alert(fieldArray);
    //alert(fieldArray[0]);
    //alert(fieldArray[1]);
    //alert(fieldArray[2]);
    //alert(fieldArray[3]);
    
    		var n = frm.length;
    		for (i = 0; i < n; i++) {
    			e = frm[i].name;
    			z = i;
    			z++;
    			var fieldType  = frm[i].type;
    			var fieldValue = fieldArray[z];
    
    			//
    			// TEXT, TEXTAREA, and DROPDOWN
    			//
    			if ((fieldType == "text") ||
    			    (fieldType == "textarea") ||
    			    (fieldType == "select-one"))
    			{
    		    	frm.elements[e].value = fieldValue;
    		    	//alert(e);
    		    	//alert(fieldValue);
    			}
    
    	
    			//
    			// HIDDEN field
    			//
    			if (fieldType == "hidden")
    			{
    		    	frm.elements[e].value = fieldValue;
    		    	}
    		}
    }
    
    /// COOKIE FUNCTIONS
    
    function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    }
    
    function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
    } else {
    begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
    end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
    }
    
    /// Text Functions
    function text() {
     for (var zxc0=0;zxc0<IdAry.length;zxc0++){
      var el=document.getElementById(IdAry[zxc0]);
      if (el){
       el.onmouseover=function() {
         changeText(this,'hide','show')
        }
       el.onmouseout=function() {
         changeText(this,'show','hide');
        }
      }
     }
    }
    function changeText(obj,cl1,cl2) {
       obj.getElementsByTagName('SPAN')[0].className=cl1;
       obj.getElementsByTagName('SPAN')[1].className=cl2;
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Well, this isn't legal syntax *AS IT STANDS*:
    getCookie:function(Name){

    Did you omit showing us the object that contains that name/value pair?

    And if those *ARE* part of some object, then your code can NOT invoke them as it trying to do.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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