424,294 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,294 IT Pros & Developers. It's quick & easy.

How to store the values of fields into memory?

100+
P: 103
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.
Oct 25 '12 #1
Share this Question
Share on Google+
8 Replies


Rabbit
Expert Mod 10K+
P: 12,303
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.
Oct 25 '12 #2

Frinavale
Expert Mod 5K+
P: 9,731
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.
Oct 25 '12 #3

100+
P: 103
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?
Oct 25 '12 #4

Rabbit
Expert Mod 10K+
P: 12,303
You set the document.cookie property to create a cookie(s). You parse it to retrieve the cookie.
Expand|Select|Wrap|Line Numbers
  1. // Set a cookie
  2. document.cookie = "username=Rabbit;"
  3.  
  4. // Read a cookie
  5. document.cookie.substr(document.cookie.indexOf("=")+1);
  6.  
Append to add additional cookies. Split by the semicolon and parse to read more than one cookie.
Oct 25 '12 #5

100+
P: 103
@Rabbit
This is what I have to create cookies, however, it doesn't seem to work.
Expand|Select|Wrap|Line Numbers
  1. function setCookie(cookieName, value)
  2. {
  3.          var expiration = new Date();
  4.          expiration.setDate(expiration.getDate() + exdays);
  5.          var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+expiration.toUTCString());
  6.          document.cookie= cookieName + "=" + c_value;
  7.  
  8. }
To read the Cookie:
Expand|Select|Wrap|Line Numbers
  1. function getCookie(cookieName)
  2. {
  3.             for (i=0; i< theCookies.length; i++)
  4.             {
  5.             x=theCookies[i].substr(0,theCookies[i].indexOf("="));
  6.             y=theCookies[i].substr(theCookies[i].indexOf("=")+1);
  7.             if(x == cookieName)
  8.             {
  9.                 return unescape(y)
  10.             }        
  11. }
This is my execution on an event handler:
Expand|Select|Wrap|Line Numbers
  1.     setCookie("Department", document.getElementById("Department").value);
  2.                         concatenateFields();---works
  3.                         document.getElementById("ShowMoreTasksButton").click();---works
  4.                         document.getElementById("Department").value = getCookie("Department").value; --the element does not get the value inserted
  5.  
Oct 25 '12 #6

Rabbit
Expert Mod 10K+
P: 12,303
Strings don't have a value property. You never define your exdays variable.
Oct 25 '12 #7

100+
P: 103
@Rabbit
Sorry, I removed that which would allow the cookie to expire on default. Default would be at the end of the session.
Oct 25 '12 #8

Rabbit
Expert Mod 10K+
P: 12,303
That's fine, but you still have that first issue I brought up.
Oct 26 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.