How to store the values of fields into memory?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    How to store the values of fields into memory?

    I have a form with a few dropdowns and a textbox. If the page had an issue and a refresh is performed, I want to repopulate the fields with the values originally entered. How would I save these values to memory. I assume I would create global variables with javascript and add the onchange event to the fields, however, I am stucking in getting this to perform.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If a page gets refreshed, those global variables will disappear as well. You can either store the values in a cookie or use AJAX to store the values on the server.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Eventually HTML5 is supposed to have client side storage but not all browsers have this implemented yet.

      So, as Rabbit suggested, use cookies for now...or store the original values on the server...or submit the current values to the server and "fix the problem" upon refreshing.

      Comment

      • Brian Connelly
        New Member
        • Jan 2011
        • 103

        #4
        My problem is now a little more complex. I wont be able to store the values on the server. Any ideas or pointers to how to create a cookie to store the value of a text input and a dropdown selected value/index?

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          You set the document.cookie property to create a cookie(s). You parse it to retrieve the cookie.
          Code:
          // Set a cookie
          document.cookie = "username=Rabbit;"
          
          // Read a cookie
          document.cookie.substr(document.cookie.indexOf("=")+1);
          Append to add additional cookies. Split by the semicolon and parse to read more than one cookie.

          Comment

          • Brian Connelly
            New Member
            • Jan 2011
            • 103

            #6
            Originally posted by Rabbit
            You set the document.cookie property to create a cookie(s). You parse it to retrieve the cookie.
            Code:
            // Set a cookie
            document.cookie = "username=Rabbit;"
            
            // Read a cookie
            document.cookie.substr(document.cookie.indexOf("=")+1);
            Append to add additional cookies. Split by the semicolon and parse to read more than one cookie.
            This is what I have to create cookies, however, it doesn't seem to work.
            Code:
            function setCookie(cookieName, value)
            {
            		 var expiration = new Date();
            		 expiration.setDate(expiration.getDate() + exdays);
            		 var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+expiration.toUTCString());
            		 document.cookie= cookieName + "=" + c_value;
            	
            }
            To read the Cookie:
            Code:
            function getCookie(cookieName)
            {
            			for (i=0; i< theCookies.length; i++)
            			{
            			x=theCookies[i].substr(0,theCookies[i].indexOf("="));
            			y=theCookies[i].substr(theCookies[i].indexOf("=")+1);
            			if(x == cookieName)
            			{
            				return unescape(y)
            			}		
            }
            This is my execution on an event handler:
            Code:
            	setCookie("Department", document.getElementById("Department").value);
            						concatenateFields();---works
            						document.getElementById("ShowMoreTasksButton").click();---works
            						document.getElementById("Department").value = getCookie("Department").value; --the element does not get the value inserted

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Strings don't have a value property. You never define your exdays variable.

              Comment

              • Brian Connelly
                New Member
                • Jan 2011
                • 103

                #8
                Originally posted by Rabbit
                Strings don't have a value property. You never define your exdays variable.
                Sorry, I removed that which would allow the cookie to expire on default. Default would be at the end of the session.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  That's fine, but you still have that first issue I brought up.

                  Comment

                  Working...