PDA

View Full Version : Recall Form Values Script II



trippin
02-11-2007, 06:31 PM
1) Script Title:
Recall Form Values Script II
2) Script URL (on DD):
http://www.dynamicdrive.com/dynamicindex16/formremember2.htm
3) Describe problem:
http://www.dynamicdrive.com/forums/showthread.php?t=17241

jscheuer1
02-11-2007, 11:13 PM
Try this out:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function createCookie(name,value,days) {
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 nameEQ = name + "=";
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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

function retrieve_password(){
if(readCookie('pwrd')){
var thepass=document.getElementsByTagName('input');
for (var i_tem = 0; i_tem < thepass.length; i_tem++)
if(thepass[i_tem].type=='password')
thepass[i_tem].value=readCookie('pwrd');
}
}

function save_password(){
if(document.getElementById('spw').checked){
var thepass=document.getElementsByTagName('input');
for (var i_tem = 0; i_tem < thepass.length; i_tem++)
if(thepass[i_tem].type=='password')
createCookie('pwrd',thepass[i_tem].value)
}
else
eraseCookie('pwrd')
}

if ( typeof window.addEventListener != "undefined" )
window.addEventListener( "load", retrieve_password, false );
else if ( typeof window.attachEvent != "undefined" )
window.attachEvent( "onload", retrieve_password );
else {
if ( window.onload != null ) {
var oldOnload = window.onload;
window.onload = function ( e ) {
oldOnload( e );
retrieve_password();
};
}
else
window.onload = retrieve_password;
}

if ( typeof window.addEventListener != "undefined" )
window.addEventListener( "unload", save_password, false );
else if ( typeof window.attachEvent != "undefined" )
window.attachEvent( "onunload", save_password );
else {
if ( window.onunload != null ) {
var oldOnunload = window.onunload;
window.onunload = function ( e ) {
oldOnunload( e );
save_password();
};
}
else
window.onunload = save_password;
}
</script>
</head>
<body>
<form action="">
<input type="password" name=""><br>
save password?<input type="checkbox" id="spw">
</form>
</body>
</html>

trippin
02-12-2007, 04:09 AM
Thanks that worked beautifully!
Now how would I go about adding another text input above the password input and have them both write to the same cookie?

jscheuer1
02-12-2007, 04:32 AM
My thinking was that rather than rewrite other saving scripts into this one, this script could be used along with pretty much any other script that does that sort of thing (saves text fields or whatever).

trippin
02-12-2007, 08:01 AM
So what is the difference between this script and the one here (http://www.dynamicdrive.com/forums/showthread.php?t=8821&highlight=recall+form) which enables it to work???

jscheuer1
02-12-2007, 08:04 AM
So what is the difference between this script and the one here (http://www.dynamicdrive.com/forums/showthread.php?t=8821&highlight=recall+form) which enables it to work???

The main difference is that I wrote the script in this thread from scratch with the specific purpose in mind of saving the password. The other script you are referencing was written by someone else, I believe specifically to save text field values.

trippin
02-12-2007, 09:36 AM
The reason I asked this is because I have been messin around with your code and can't get a text input to write to this same cookie. I've tried using it along with the Form Recall Script II. When I do this only the password is saved by your script.

I am using a form like so:

<form name="manlog" action="/">ID:
<input type="text" name="login"><BR>PW:
<input type="password" name="passwd"><br>
Save Login?<input type="checkbox" id="persistbox"><br>
<input type="submit" value="Login">
</form>

Also I noticed that the status of the checkbox was not being recorded to cookie so when I leave the site and come back the cookie is erased because the checkbox becomes empty.

trippin
02-12-2007, 01:11 PM
I did it! This script will save ALL fields or SPECIFIC fields in the form you specify and write them to a cookie. I had a lot of problems with trying to write the password value to cookie but not anymore.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="logincookie.js" type="text/javascript"></script>
</head>
<body onload="recoverInputs(document.forms.manlog,retrieveCookie('y_login'),true);">
<form name="manlog" action="/">ID:
<input type="text" name="login"><BR>PW:
<input type="password" name="passwd"><br>
Save Login?<input type="checkbox" id="remember"><br>
<input type="reset" name="g">

<input type="button" value="Save" onclick="rememberme()">
<input type="button" value="Recover" onclick="recoverInputs(this.form,retrieveCookie('y_login'),true);">
</form>



</body>
</html>