I wanted to share this if anyone needs this little tidbit. If you want to use a cookie to pre-fill fields, take a look at http://www.tfaforms.com/128200
This form has JavaScript to allow for you to pre-fill the text fields with data from cookies. This will allow users to have text fields pre-filled say for example with their name and address. Cookies are commonly used in this fashion to enhance the user experience.
Some words of caution: This is advanced coding and not for a beginner. You will need some understanding of JavaScript in order to understand the code. If you wish to gain further understanding of JavaScript, you may go to: http://www.w3schools.com/js/default.asp
If you use the code below, please remember the following: 1. Cookies only work if the Web Browser one is using to fill out the form allows cookies. 2. If you fill out a form on one PC, your data that is saved in a cookie will not follow you to a different PC or different browser. 3. If your form might be used in a public place, it may be wise not to use cookies. 4. When the date expires on the cookie, the data is lost. One would need to fill out the form again for the data to be saved to a new cookie. Note: The loss of the cookie does NOT affect any submitted forms. The cookie only allows for the pre-filling of text fields to save the user time when they are filling out a form to be submitted. 5. My example below is only designed to work with text fields. It will not work with other types of fields unless modified.
function newCookie(name,value,days) { var days = 365; // the number at the left reflects the number of days for the cookie to last // modify it according to your needs ///////////////////////////////////////////////////////////////////////////////// // Nothing in this section needs to be modified when adding or deleting fields // // in FormAssembly. // ///////////////////////////////////////////////////////////////////////////////// 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+"="+value+expires+"; path=/"; }
function readCookie(name) { var nameSG = name + "="; var nuller = ''; if (document.cookie.indexOf(nameSG) == -1) return nuller;
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(nameSG) == 0) return c.substring(nameSG.length,c.length); } return null; }
function eraseCookie(name) { newCookie(name,"",1); }
////////////////////////////////////////////////////////////////////////////////// // End section // //////////////////////////////////////////////////////////////////////////////////
/* Notes: 1. I use the form ID name as the cookie name as well as any references to the field. 2. The cookie only gets written to when the page is unloaded. 3. The addLoadEvent function is handy for adding multiple events when the page loads. 4. This code is designed to work with text fields. To work with Picklist, radio buttons and so forth will require one to make adjustments to the code. */
///////////// // Add field IDs here. Each must be surrounded by single quotes // and be separated by commas. var aFields = new Array('tfa_Username','tfa_Address','tfa_Phone'); ///////////// function toMem() { for (x in aFields){ newCookie(aFields[x], document.getElementById(aFields[x]).value); } }
function delMem() { for (x in aFields){ eraseCookie(aFields[x]); document.getElementById(aFields[x]).value = ''; } }
function remCookie() { for (x in aFields){ document.getElementById(aFields[x]).value = readCookie(aFields[x]); } }
function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } }
addLoadEvent(function() { remCookie(); }); </script> <body onunload=toMem()> <!- When the page is unloaded, the fields are read and the info gathered is written to the cookies ->