Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using JS cookie to recall form fields - eliminate "hidden" fields

Status
Not open for further replies.

Overspeed

Programmer
Jul 10, 2007
15
CA
Hello,

I am using the following script to record inside a cookie form elements that can be recalled on a future visit. I works great but it is recording "hidden" fields as well as standard input fields and this causes big problems for its application. Here is the script:

var expDays = 100;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function cookieForms() {
var mode = cookieForms.arguments[0];
for(f=1; f<cookieForms.arguments.length; f++) {
formName = cookieForms.arguments[f];
if(mode == 'open') {
cookieValue = GetCookie('saved_'+formName);
if(cookieValue != null) {
var cookieArray = cookieValue.split('#cf#');
if(cookieArray.length == document[formName].elements.length) {
for(i=0; i<document[formName].elements.length; i++) {
if(cookieArray.substring(0,6) == 'select') { document[formName].elements.options.selectedIndex = cookieArray.substring(7, cookieArray.length-1); }
else if((cookieArray == 'cbtrue') || (cookieArray == 'rbtrue')) { document[formName].elements.checked = true; }
else if((cookieArray == 'cbfalse') || (cookieArray == 'rbfalse')) { document[formName].elements.checked = false; }
else { document[formName].elements.value = (cookieArray) ? cookieArray : ''; }
}
}
}
}
if(mode == 'save') {
cookieValue = '';
for(i=0; i<document[formName].elements.length; i++) {
fieldType = document[formName].elements.type;
if(fieldType == 'password') { passValue = ''; }
else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements.checked; }
else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements.checked; }
else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements.options.selectedIndex; }
else { passValue = document[formName].elements.value; }
cookieValue = cookieValue + passValue + '#cf#';
}
cookieValue = cookieValue.substring(0, cookieValue.length-4);
SetCookie('saved_'+formName, cookieValue, exp);
}
}
}

The script is called via this <BODY> command:

<body onload="cookieForms('open', 'calculations'); showButton();" onunload="cookieForms('save', 'calculations')">

Is there a way that the above script could be modified so that is only records the visible input fields and ignores the hidden ones? Thank you.

Hugz

Jenny
 
Hi

Assuming you mean to skip [tt]form[/tt] elements with the [tt]visibility[/tt] style property set to [tt]hidden[/tt] :
Code:
[gray]...[/gray]
    if(mode == 'save') {    
      cookieValue = '';
      for(i=0; i<document[formName].elements.length; i++) {
[red]if (document[formName].elements[i].style.visibility=='hidden') continue;[/red]
        fieldType = document[formName].elements[i].type;
        if(fieldType == 'password') { passValue = ''; }
        else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
[gray]...[/gray]

Feherke.
 
Hi Feherke,

Sorry, I was not referring to form elements that use CSS visibility, instead form values that are designated as "hidden" like this:

<input type="hidden" name="date" value="071507">

The script above as originally designed will record ALL form elements both user input and hidden. I would like the hidden fields to not be written to the cookie. You see, some of the hidden fields are dynamicly generated and I need them to be different each time the page loads. Even though in the html the hidden fields always differ, the cookie saving script overrides them during a submit.

I hope you can understand. Thank you for your reply.

Jenny
 
Hi

Code:
[gray]...[/gray]
    if(mode == 'save') {    
      cookieValue = '';
      for(i=0; i<document[formName].elements.length; i++) {
        fieldType = document[formName].elements[i].type;
[red]if (fieldType=='hidden') continue;[/red]
        if(fieldType == 'password') { passValue = ''; }
        else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
[gray]...[/gray]

Feherke.
 
if (fieldType=='hidden') continue;

I tried that and it had no effect :(

J
 
Hi

Well, although I was sure it works, I tried it. And yes, it works : it does not save the value of [tt]hidden[/tt] [tt]input[/tt]s.

However, if you want to stop the strange behavior of reseting values of unsaved [tt]input[/tt]s, you will need this too :
Code:
[gray]...[/gray]
        var cookieArray = cookieValue.split('#cf#');
        if(cookieArray.length == document[formName].elements.length) {
          for(i=0; i<document[formName].elements.length; i++) {
[red]if (document[formName].elements[i].type=='hidden') continue;[/red]
            if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
            else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { 
[gray]...[/gray]

Feherke.
 
Feherke,

Let me elaborate on my last response. When I modify the script as you last suggested there is a change ... when I view the cookie contents I no longer see the hidden values included. I do see the <select> values recorded ... but ... they are not repopulating to the page when revisited. Let me explain ...

When the original script is used the cookie records ALL form field types and repopulates the values back to the form when next visited. When I use your modified version, the hidden field types are not being recorded (good) but the select fields are not being repopulated. I'm studying this issues but no results yet.

J
 
Feherke,

Ok I think its working now. Your third solution seems to be the magic one. Your second solution DOES prevent the recording of the hidden fields but it causes a glitch in the repopulation of the form fields. Your third solution tells the "open cookie" function to simply ignore the hidden fields that are present in the data. I will test further but so far looks great.

P.S. I now owe you two favors!

Jenny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top