JavaScript into PERL CGI coding help
by "Gary E. Daniels" <gdaniels(at)ais.net>
|
Date: |
Fri, 21 Jul 2000 09:57:30 -0500 |
To: |
hwg-languages(at)hwg.org |
|
todo: View
Thread,
Original
|
|
I have a JavaScript that uses cookies to retain input from a form so it can
re-populate the form fields or another form with the same name and fields.
This works fine when the forms are HTML. My problem is getting it to work
when the JavaScript is pasted into a PERL CGI program that creates the HTML
form on-the-fly. I get Internal Server Errors and I suspect that I need to
add some additional PERL coding such as Print statements, newline
characters, quotes, etc. The full JavaScript appears below with a sample
HTML form. Thanks in advance.
<HTML>
<title>Your title goes here!</title>
<BODY bgcolor="white">
<!-- Cut-N-Paste JavaScript from ISN Toolbox
Copyright 1998, Infohiway, Inc. Restricted use is hereby
granted (commercial and personal OK) so long as this code
is not *directly* sold and the copyright notice is buried
somewhere deep in your HTML document. A link to our site
http://www.infohiway.com is always appreciated of course,
but is absolutely and positively not necessary. ;-) -->
<SCRIPT language="JavaScript">
<!--
var never = new Date()
never.setTime(never.getTime() + 2000*24*60*60*1000);
// name is a string of the name of your cookie
// value is the value corresponding to name
function SetCookie(name, value) {
var expString = "; expires=" + never.toGMTString();
document.cookie = name + "=" + escape(value) + expString;
}
// returns value of cookie or null if cookie does not exist
function GetCookie(name) {
var result = null;
var myCookie = " " + document.cookie + ";";
var searchName = " " + name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1) {
startOfCookie += searchName.length; // skip past name of cookie
endOfCookie = myCookie.indexOf(";", startOfCookie);
result = unescape(myCookie.substring(startOfCookie, endOfCookie));
}
return result;
}
use_cookies = "unsure";
function saveValue(element) {
if (document.images && use_cookies == "unsure")
use_cookies = (confirm("Will you allow the values you enter into this form "
+"to be stored as cookies so that those values will be pre-filled the next "
+"time you return to this page?") ? "yes":"no");
if (document.images && use_cookies == "yes") {
if ((element.type == "text")
|| (element.type == "password")
|| (element.type == "textarea")
|| (element.type == "radio")) {
val = element.value;
} else if (element.type.indexOf("select") != -1) {
val = "";
for(k=0;k<element.length;k++)
if (element.options[k].selected)
val += k+" ";
} else if (element.type == "checkbox") {
val = element.checked;
}
SetCookie("memory_"+element.form.name+"_"+element.name,val);
}
}
function storedValues() {
if (document.images) { // only do it in JavaScript 1.1 browsers
for (i=0;i<document.forms.length;i++) {
for (j=0;j<document.forms[i].elements.length; j++) {
cookie_name = "memory_"+document.forms[i].name+"_"
+document.forms[i].elements[j].name;
val = GetCookie(cookie_name);
if (val) {
if ((document.forms[i].elements[j].type == "text")
|| (document.forms[i].elements[j].type == "password")
|| (document.forms[i].elements[j].type == "textarea")) {
document.forms[i].elements[j].value = val;
} else if (document.forms[i].elements[j].type.indexOf("select") != -1) {
document.forms[i].elements[j].selectedIndex = -1;
while (((pos = val.indexOf(" ")) != -1) && (val.length > 1)) {
sel = parseInt(val.substring(0,pos));
val = val.substring(pos+1,val.length);
if (sel < document.forms[i].elements[j].length)
document.forms[i].elements[j].options[sel].selected = true;
}
} else if (document.forms[i].elements[j].type == "checkbox") {
document.forms[i].elements[j].checked = val;
} else if (document.forms[i].elements[j].type == "radio") {
if (document.forms[i].elements[j].value == val)
document.forms[i].elements[j].checked = true;
}
}
}
}
}
}
window.onload = storedValues;
// -->
</SCRIPT>
Sample form:<br><br>
<form name="test">
Name: <input type="text" name="fullname" onChange="saveValue(this)" size=30><br>
Password: <input type="password" name="password" onChange="saveValue(this)"
size=30><br>
<input type="radio" name="spouse" onClick="saveValue(this)" value="married">
Married
<input type="radio" name="spouse" onClick="saveValue(this)" value="single">
Single<br>
<input type="checkbox" name="likeit" value="yes" onClick="saveValue(this)">
I like this script<br>
Hometown: <input type="text" name="nothing" size=30><br>
<i>("Hometown" doesn't remember what you enter in it)</i><br><br>
Age:<br>
<select name="age" onChange="saveValue(this)" size=1>
<option selected>0-10
<option>11-20
<option>21-30
<option>31-40
<option>41-50
<option>51-60
<option>61-70
<option>over 70
</select><br><br>
I use the following browsers: (select all that apply)<br>
<select name="browser" onChange="saveValue(this)" size=3 multiple>
<option>Netscape Navigator
<option>Microsoft Internet Explorer
<option>Other
</select><br><br>
Other Comments:<br>
<textarea name="comments" rows=4 cols=60 onChange="saveValue(this)"></textarea>
</form>
Script from <a href="http://www.infohiway.com/javascript?memory">Cut and
Paste JavaScript</a>.
</body>
</html>
Gary E. Daniels
Cornerstone Appraiser Company
500 Selkirk Drive
Schaumburg, IL. 60194
847 882-5892 Fax 847 882-5963
mailto:gdaniels(at)ais.net
Online Appraisal Order Form and ASK Special Purpose Property Refferal
Database at
http://www.askdata.net
HWG: hwg-languages mailing list archives,
maintained by Webmasters @ IWA