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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving form in Javascript

Status
Not open for further replies.

guestAsh

Technical User
Joined
Feb 27, 2004
Messages
65
Location
GB
Hi,

I use the following Javascript to save the form fields (so i can re-load the page, without the users re-en


Code:
<script LANGUAGE="JavaScript">

// Cookie Functions  ////////////////////  (:)

// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);


// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');

var expDays = 0.01;

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" : "");

}



// cookieForms saves form content of a page.



// use the following code to call it:

//  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">

// It works on text fields and dropdowns in IE 5+

// It only works on text fields in Netscape 4.5


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[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')) { document[formName].elements[i].checked = true; }

						else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }

						else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }

					}

				}

			}

		}

				

		if(mode == 'save') {	

			cookieValue = '';

			for(i=0; i<document[formName].elements.length; i++) {

				fieldType = document[formName].elements[i].type;
				
				if(fieldType == 'password') { passValue = ''; }
				
			
				else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }

				else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }

				else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }

				else { passValue = document[formName].elements[i].value; }

			

				cookieValue = cookieValue + passValue + '#cf#';

			}

			cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter

			

			//SetCookie('saved_'+formName, cookieValue, exp);	
			SetCookie('saved_'+formName, cookieValue);		
	

		}	

	}

}

//  End -->

</script>
 
Was there a question in there somewhere?

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
yeah sorry! My PC at work decided to do some strange stuff, and crashed!

here's the question:

I use the Above Javascript to save the form fields (so i can re-load the page, without the users losing any data they have entered into the form)

but is there away so i can skip a "named" textbox? so it dosn't save the value of that textbox

something along the lines of: "IF TEXTBOX NAME = "FRED" THEN DONT WRITE VALUE TO COOKIE.

does that make sense?

Cheers

Ash
 
Sure. In between these two lines:

Code:
for(i=0; i<document[formName].elements.length; i++) {
   fieldType = document[formName].elements[i].type;

add this line:

Code:
if (document[formName].elements[i].name == 'fred') continue;

So all up, the first few lines of the "save" routine would be:

Code:
if (mode == 'save') {    
	cookieValue = '';
	for(i=0; i<document[formName].elements.length; i++) {
		[!]if (document[formName].elements[i].name == 'fred') continue;[/!]
		fieldType = document[formName].elements[i].type;

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan,

But...

The code only saves the value for the form element that is named "fred". Is it possible so it saves all the other form elements apart from the form element called "fred"???

cheers

Ash
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top