»
EnglishFrenchVietnamese

Print - Save and Restore Form Cookies - JavaScriptBank.com

Full version: jsB@nk » Cookie » Save and Restore Form Cookies
URL: https://www.javascriptbank.com/save-and-restore-form-cookies.html

Save and Restore Form Cookies © JavaScriptBank.comThis JavaScript will save all of your form fields in a cookie. It includes all text fields, radio button selections, checkbox values, etc. When your visitor returns, all form fields are automatically repopulated with the same value that they entered on their last visit.

Full version: jsB@nk » Cookie » Save and Restore Form Cookies
URL: https://www.javascriptbank.com/save-and-restore-form-cookies.html



JavaScript
<script type="text/javascript" name=saveRestore.js>// Created by: Tony Davis :: http://www.tdavisconsulting.com/* SAVE FORM FIELD SELECTIONS IN COOKIES ------------------------------------------------This version will concatentate all the fields in        in the form into one string, delimited by a PIPEsymbol, into one cookie. The cookie name is the same name as the form name. ALL fields are saved. */function saveSelections(frm) {  var setvalue;  var fieldType;  var index;  var formname = frm.id;                                    // 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);    //    // RADIO BUTTON    //    if (fieldType == "radio") {    //alert(frm.elements[e].length);      for (x=0; x < frm.elements[e].length; x++) {        if (frm.elements[e][x].checked)  {        index = x        }      }    string = string + index + "\|";    }    //    // TEXT, TEXTAREA, and DROPDOWN    //    if ((fieldType == "text") ||       (fieldType == "textarea") ||      (fieldType == "select-one")) {    string = string + frm.elements[e].value + "\|";    //alert("text");    }    //    // CHECKBOX    //    if (fieldType == "checkbox")     {      if (frm.elements[e].checked==true) {        var setvalue = "1";        }      if (frm.elements[e].checked==false) {        var setvalue = "0";        }    string = string + setvalue + "\|";    //alert("checkbox");    }  }  //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);  }    //    // CHECKBOX    //    if (fieldType == "checkbox") {      fld_checkbox = fieldValue;      if (fld_checkbox == "1") {          frm.elements[e].checked = true;      }    }    //    // RADIO BUTTON    //    if (fieldType == "radio") {      x = fieldValue;      //alert(x);      frm.elements[e][x].checked = true;    }  }}</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->