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

How whould I preload user data on FORM? via cookies?

Status
Not open for further replies.

JCHallgren

Technical User
Dec 17, 2004
47
0
0
US
Newbie + First time post here so be gentle, please..

I have posted this in another forum but wanted the JS input also...I have one days knowledge of JS so...

I have a simple guestbook that uses a input FORM in HTML to get data...and then a Perl CGI to process it...(which works great!) I would only need to be able to somehow put in a value for the NAME/CITY/ST fields when the ADD form is first displayed (IF they had done so before at my site using this same FORM)

Currently, the ADD form is just a plain HTML page...

If you can give me a sample (or links to where to see one)of how I would do one field, I can obviously adapt to others...and would be MOST grateful!
 
Code:
<script language=javascript>
	function fillForm(){
		if (document.cookie == "") return
		theCookie = document.cookie.split("; ")
		for (x=0; x<theCookie.length; x++){
    			field = eval("document.myForm." +  theCookie[x].split("=")[0])
    			field.value = eval("document.myForm." +  theCookie[x].split("=")[1])
		}
	}

	function saveForm(){
		expireDate = new Date()
		expireDate.setMonth(expireDate.getMonth() + 6)
		document.cookie = "uName=" + document.myForm.uName.value +  ";expires=" + expireDate.tpGMTString()
		document.cookie = "city=" + document.myForm.city.value +  ";expires=" + expireDate.tpGMTString()
	}

</script>



<body onLoad="fillForm()" onUnload="saveForm()">
<form name=myForm>
<input name=uName>
<input name=city>
</form>
</body>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
zen.gif

 
THANK you VERY much for your sample! Even with one days JS knowledge, it was possible to follow it! (Given the primitive skills in Perl that I now have)

My follow-up question is: This will have NO effect on my existing post-processing of the FORM data via my CGI/Perl script executed via Action parm, correct? Just making 100% sure...
 
That is correct

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
zen.gif

 
So my only downside risk, compared to other options to do it such as PHP, is the users who have no Javascript ability, such as those who have it turned off or similar?

And would I have to be concerned with users input that may (if being nasty) contain all sorts of special chars that my Perl pgm will screen out? That is, must I do any edits in JS first?

And I again wish to COMPLIMENT you on your clear quick reply! There are a few syntax things I don't follow totally, but I will first look those up on my own before I ask.
 
After much effort, i got my cookies working! There were a few minor but critical errors in the code given...I suspect one was just a typo...thus it took me quite a number of hours to get it going...however, I also adapted it to a slightly different style of usage...as found on another site...that is, I combine all fields into one single cookie "write"...and also use escape/unescape to make sure "my" delimeters are found when I extract the various fields...

I will post the resulting code when I have chance...
 
As promised, here is what I ended up with....

The issues I had with mwolf00's code included:
1) it had tpGMTstring instead of toGMTstring
2) it used onUnload instead of onSubmit (critical!)
3) almost all cookie writes (I found later) use escape/unescape on value to prevent unexpected separators
4) setting field.value appeared to have an unneeded Eval

By adapting some other code, I ended up with ONE consolidated string that made create/retrieval a bit easier IMHO

(Found some info on web, maybe outdated, that implied that in some cases, cookie blockers may change document.cookie to document.ignore so thus the check for string)
-------------
As promised, here is what I ended up with....

The issues I had with mwolf00's code included:
1) it had tpGMTstring instead of toGMTstring
2) it used onUnload instead of onSubmit (critical!)
3) almost all cookie writes (I found later) use escape/unescape on value to prevent unexpected separators
4) setting field.value appeared to have an unneeded Eval

By adapting some other code, I ended up with ONE consolidated string that made create/retrieval a bit easier IMHO

(Found some info on web, maybe outdated, that implied that in some cases, cookie blockers may change document.cookie to document.ignore so thus the check for string)
-------------
Code:
function fillCRuser(){
if(typeof document.cookie == "string"){
if (document.cookie == "")	{ 
    document.CRaddForm.CRuserName.focus();
    return; 	}
   var CD = document.cookie.split("=")[1];
   var CDarray = CD.split("|");	
  for (x=0; x<CDarray.length; x++){
    CDsplit = CDarray[x].split(":");
    CDvalue = unescape(CDsplit[1]);
    CDitem = eval("document.CRaddForm." + CDsplit[0]);	
    CDitem.value = CDvalue;	}
    document.CRaddForm.CRuserComm.focus() }
else {
   document.CRaddForm.CRuserName.focus() }	
}	
/* ************************************************* */
/* Formats & create COOKIE with user entered fields */
function saveCRuser(){
  if(typeof document.cookie == "string"){
   var CD  = "CRuserName:"  + escape(document.CRaddForm.CRuserName.value )	+ "|"
				+ "CRuserEmail:" + escape(document.CRaddForm.CRuserEmail.value) + "|"
				+ "CRuserCity:"  + escape(document.CRaddForm.CRuserCity.value )	+ "|"
				+ "CRuserState:" + escape(document.CRaddForm.CRuserState.value);
		expireDate = new Date();
		expireDate.setMonth(expireDate.getMonth() + 6);
		document.cookie = "CRdata=" + CD +  ";expires=" + expireDate.toGMTString();
    }//else document.cookie is not string so bypass write.
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top