Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
need help with cookie (and more)
Old 04-16-2007, 03:53 AM need help with cookie (and more)
Junior Talker

Posts: 1
Trades: 0
Hi all!

I'm pretty new to all this php so I've some a pre-written php I can use.

Problem is I need to make some changes and I can't figure the code out!

Basically it is a cookie that "remembers" some input.

I would like this input to be remembered for 24 hours.

Second, I want it to remember the input with "body onunload" and load it again (body onload) next time I go to the page.

here you can see what I mean: http://www.onlinemoneygroup.com/dave/test.htm

it works with the buttons, but is not "remembering" the input when broser is closed.

here are the cookie.js file:

Code:
function retrieveCookie( cookieName ) {
 /* retrieved in the format
 cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
 only cookies for this domain and path will be retrieved */
 var cookieJar = document.cookie.split( "; " );
 for( var x = 0; x < cookieJar.length; x++ ) {
  var oneCookie = cookieJar[x].split( "=" );
  if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
 }
 return null;
}
function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
 if( !cookieName ) { return false; }
 if( lifeTime == "delete" ) { lifeTime = -10; } //this is in the past. Expires immediately.
 /* This next line sets the cookie but does not overwrite other cookies.
 syntax: cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]]
 Because of the way that document.cookie behaves, writing this here is equivalent to writing
 document.cookie = whatIAmWritingNow + "; " + document.cookie; */
 document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
  ( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
  ( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
  ( isSecure ? ";secure" : "");
 //check if the cookie has been set/deleted as required
 if( lifeTime < 0 ) { if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return false; } return true; }
 if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return true; } return false;
}
and the saveFormValues.js:

Code:
var FS_INCLUDE_NAMES = 0, FS_EXCLUDE_NAMES = 1, FS_INCLUDE_IDS = 2, FS_EXCLUDE_IDS = 3, FS_INCLUDE_CLASSES = 4, FS_EXCLUDE_CLASSES = 5;
function getFormString( formRef, oAndPass, oTypes, oNames ) {
 if( oNames ) {
  oNames = new RegExp((( oTypes > 3 )?'\\b(':'^(')+oNames.replace(/([\\\/\[\]\(\)\.\+\*\{\}\?\^\$\|])/g,'\\$1').replace(/,/g,'|')+(( oTypes > 3 )?')\\b':')$'),'');
  var oExclude = oTypes % 2;
 }
 for( var x = 0, oStr = '', y = false; formRef.elements[x]; x++ ) {
  if( formRef.elements[x].type ) {
   if( oNames ) {
    var theAttr = ( oTypes > 3 ) ? formRef.elements[x].className : ( ( oTypes > 1 ) ? formRef.elements[x].id : formRef.elements[x].name );
    if( ( oExclude && theAttr && theAttr.match(oNames) ) || ( !oExclude && !( theAttr && theAttr.match(oNames) ) ) ) { continue; }
   }
   var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
   if( oT == 'text' || oT == 'textarea' || ( oT == 'password' && oAndPass ) || oT == 'datetime' || oT == 'datetime-local' || oT == 'date' || oT == 'month' || oT == 'week' || oT == 'time' || oT == 'number' || oT == 'range' || oT == 'email' || oT == 'url' ) {
    oStr += ( y ? ',' : '' ) + oE.value.replace(/%/g,'%p').replace(/,/g,'%c');
    y = true;
   } else if( oT == 'radio' || oT == 'checkbox' ) {
    oStr += ( y ? ',' : '' ) + ( oE.checked ? '1' : '' );
    y = true;
   } else if( oT == 'select-one' ) {
    oStr += ( y ? ',' : '' ) + oE.selectedIndex;
    y = true;
   } else if( oT == 'select-multiple' ) {
    for( var oO = oE.options, i = 0; oO[i]; i++ ) {
     oStr += ( y ? ',' : '' ) + ( oO[i].selected ? '1' : '' );
     y = true;
    }
   }
  }
 }
 return oStr;
}
function recoverInputs( formRef, oStr, oAndPass, oTypes, oNames ) {
 if( oStr ) {
  oStr = oStr.split( ',' );
  if( oNames ) {
   oNames = new RegExp((( oTypes > 3 )?'\\b(':'^(')+oNames.replace(/([\\\/\[\]\(\)\.\+\*\{\}\?\^\$\|])/g,'\\$1').replace(/,/g,'|')+(( oTypes > 3 )?')\\b':')$'),'');
   var oExclude = oTypes % 2;
  }
  for( var x = 0, y = 0; formRef.elements[x]; x++ ) {
   if( formRef.elements[x].type ) {
    if( oNames ) {
     var theAttr = ( oTypes > 3 ) ? formRef.elements[x].className : ( ( oTypes > 1 ) ? formRef.elements[x].id : formRef.elements[x].name );
     if( ( oExclude && theAttr && theAttr.match(oNames) ) || ( !oExclude && ( !theAttr || !theAttr.match(oNames) ) ) ) { continue; }
    }
    var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
    if( oT == 'text' || oT == 'textarea' || ( oT == 'password' && oAndPass ) || oT == 'datetime' || oT == 'datetime-local' || oT == 'date' || oT == 'month' || oT == 'week' || oT == 'time' || oT == 'number' || oT == 'range' || oT == 'email' || oT == 'url' ) {
     oE.value = oStr[y].replace(/%c/g,',').replace(/%p/g,'%');
     y++;
    } else if( oT == 'radio' || oT == 'checkbox' ) {
     oE.checked = oStr[y] ? true : false;
     y++;
    } else if( oT == 'select-one' ) {
     oE.selectedIndex = parseInt( oStr[y] );
     y++;
    } else if( oT == 'select-multiple' ) {
     for( var oO = oE.options, i = 0; oO[i]; i++ ) {
      oO[i].selected = oStr[y] ? true : false;
      y++;
     }
    }
   }
  }
 }
}
Hope someone can help me out here:-)

Casper
xwishmasterx is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Reply     « Reply to need help with cookie (and more)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.07867 seconds with 12 queries