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

Still need help w/javascripts and forms - Confused :(

Status
Not open for further replies.

nalbiso

Programmer
Aug 31, 2000
71
US
Someone help! I would like to get a script together that will require a user to input information such as name, address, phone..etc and store it as a cookie so that when they return to the page, the information is prefilled for the next time that they use the form. I know that my naming convention is wrong, but I don't understand why because I am new at this. Please look this over and explain to me how to fix it in simple terms.

Thanks!


<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
function Get_Cookie(name) {
var start = document.cookie.indexOf(name+&quot;=&quot;);
var len = start+name.length+1;
if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
if (start == -1) return null;
var end = document.cookie.indexOf(&quot;;&quot;,len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + &quot;=&quot; +escape(value) +
( (expires) ? &quot;;expires=&quot; + expires.toGMTString() : &quot;&quot;) +
( (path) ? &quot;;path=&quot; + path : &quot;&quot;) +
( (domain) ? &quot;;domain=&quot; + domain : &quot;&quot;) +
( (secure) ? &quot;;secure&quot; : &quot;&quot;);
}
function setupForm() {
if (userProfile) getValues(userProfile);
}
function getValues(string) {
getValue(string,&quot;FirstLastname&quot;, document.pts01reg.FirstLastname, &quot;text&quot;);
getValue(string,&quot;PhoneNumber&quot;, document.pts01reg.PhoneNumber, &quot;text&quot;);
getValue(string,&quot;EmailAddress&quot;, document.pts01reg.EmailAddress, &quot;text&quot;);
getValue(string,&quot;Location&quot;, document.pts01reg.Location, &quot;text&quot;);
getValue(string,&quot;Program_Title&quot;, document.pts01reg.Program_Title. &quot;select&quot;);
for (var i=0;i<7+1;i++)
getValue(string,&quot;i&quot;+i, eval(&quot;document.pts01reg.i&quot;+i), &quot;checkbox&quot;);
}
function replace(string,text,by) {
// Replaces text with by in string
var i = string.indexOf(text);
var newstr = '';
if ((!i) || (i == -1)) return string;
newstr += string.substring(0,i) + by;

if (i+text.length < string.length)
newstr += replace(string.substring(i+text.length,string.length),text,by);

return newstr;
}
function onCheck(string) { if (string == &quot;on&quot;) return true; return false; }
function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType

var startPos = string.indexOf(elementName + &quot;=&quot;)

if (startPos > -1) {
startPos = startPos + elementName.length + 1;
var endPos = string.indexOf(&quot;&&quot;,startPos);
if (endPos == -1) endPos = string.length;

var elementValue = unescape(string.substring(startPos,endPos));

if (elementType == &quot;text&quot;) object.value = elementValue;
if (elementType == &quot;password&quot;) object.value = elementValue;
if (elementType == &quot;select&quot;) object.selectedIndex = elementValue;
if (elementType == &quot;checkbox&quot;) object.checked = onCheck(elementValue);
if (elementType == &quot;radio&quot;) object[elementValue].checked = true;
}
}
//--></SCRIPT>
</head>
<BODY onLoad=&quot;setupForm()&quot; background=&quot;lt%20background.gif&quot; bgproperties=&quot;fixed&quot;>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
var today = new Date();
var expires = new Date(today.getTime() + (365 * 24 * 60 * 60 * 1000));

var searchString = replace(self.location.search.substring(1),&quot;+&quot;,&quot; &quot;);
if (searchString.length > 0) Set_Cookie(&quot;userProfile&quot;,searchString,expires);
var userProfile = Get_Cookie(&quot;userProfile&quot;);

if (!userProfile) {
document.write('<P>Welcome,<P>According to your records ');
document.write('you have not set your user profile:');
}
else {
document.write('<P>Welcome back,<P>According to your records ');
document.write('the following settings are held in your profile:');
}
//--></SCRIPT> [sig][/sig]
 
where is the error appearing?

jared@aauser.com
 
Well, I know that it has something to do with the name or the path. When I run the script, it stops executing and the form remains unfilled.

I tried this once before and it worked on the other form that I was working on. But when I looked for the error using the javascript console, it was still referencing things in the form and web that I orginally used this script on. So I am thinking that the Get cookie statement is still trying to get the cookie that I created originally with this script. But I really don't know where the error is because I am still reading through books trying to learn javascript. I guess my lack of experience in this is really the problem.

This is a copy of the error messages from the console. The URL referenced in the error is incorrect. It belongs to the original form that I used this script on.:

JavaScript Error: line 34:

missing name after . operator.

getValue(string,&quot;Program_Title&quot;, document.pts01reg.Program_Title. &quot;select&quot;);
.......................................................................^

JavaScript Error: line 78:

replace is not defined.

Any help would be greatly appreciated.

Thanks. [sig][/sig]
 
first of all, when debugging javascript in Netscape, the javascript console will not get rid of previous errors. Click the &quot;clear&quot; button to get rid of the old error messages.

secondly, you've created a function with the same name as a javascript intrinsic function, (replace). This will almost always cause trouble.

third, you don't need to write a replace function. the string object already has one. [sig]<p>nick bulka<br><a href=mailto: > </a><br><a href= </a><br>Get your technical books at Bulka's Books<br>
[/sig]
 
Ok, I cleared the console but now I get nothing at all. I tried removing the replace function. It still didn't work.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
//&quot;Cookie form values storage&quot; written by Nicole Albiso
function Get_Cookie(name) {
var start = document.cookie.indexOf(name+&quot;=&quot;);
var len = start+name.length+1;
if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
if (start == -1) return null;
var end = document.cookie.indexOf(&quot;;&quot;,len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + &quot;=&quot; +escape(value) +
( (expires) ? &quot;;expires=&quot; + expires.toGMTString() : &quot;&quot;) +
( (path) ? &quot;;path=&quot; + path : &quot;&quot;) +
( (domain) ? &quot;;domain=&quot; + domain : &quot;&quot;) +
( (secure) ? &quot;;secure&quot; : &quot;&quot;);
}

function setupForm() {
if (userProfile) getValues(userProfile);
}
function getValues(string) {
getValue(string,&quot;FirstLastName&quot;, document.ptsregb.FirstLastName, &quot;text&quot;);
getValue(string,&quot;EmailAddress&quot;, document.ptsregb.EmailAddress, &quot;text&quot;);
getValue(string,&quot;Program_Title&quot;, document.ptsregb.Program_Title, &quot;select&quot;);
getValue(string,&quot;Location&quot;, document.ptsregb.Location, &quot;text&quot;);
getValue(string,&quot;PhoneNumber&quot;, document.ptsregb.PhoneNumber, &quot;text&quot;);

for (var i=0;i<7+1;i++)
getValue(string,&quot;i&quot;+i, eval(&quot;document.ptsregb.i&quot;+i), &quot;checkbox&quot;);
}
function onCheck(string) { if (string == &quot;on&quot;) return true; return false; }
function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType

var startPos = string.indexOf(elementName + &quot;=&quot;)

if (startPos > -1) {
startPos = startPos + elementName.length + 1;
var endPos = string.indexOf(&quot;&&quot;,startPos);
if (endPos == -1) endPos = string.length;

var elementValue = unescape(string.substring(startPos,endPos));

if (elementType == &quot;text&quot;) object.value = elementValue;
if (elementType == &quot;password&quot;) object.value = elementValue;
if (elementType == &quot;select&quot;) object.selectedIndex = elementValue;
if (elementType == &quot;checkbox&quot;) object.checked = onCheck(elementValue);
if (elementType == &quot;radio&quot;) object[elementValue].checked = true;
}
}

//--></SCRIPT>
<title></title>

</HEAD>
<BODY onLoad=&quot;setupForm()&quot; background=&quot;lt%20background.gif&quot; bgproperties=&quot;fixed&quot;>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
//Establishes the Cookie Expiration Date
var today = new Date();
var expires = new Date(today.getTime() + (365 * 24 * 60 * 60 * 1000)); // expires one year from now
var userProfile = Get_Cookie(&quot;userProfile&quot;);

if (!userProfile) {
document.write('<P>Welcome,<P>According to your records ');
document.write('you have not set your user profile:');
}
else {
document.write('<P>Welcome back,<P>According to your records ');
document.write('the following settings are held in your profile:');
}
//--></SCRIPT>
[sig][/sig]
 
Ok, I cleared the console but now I get nothing at all. I tried removing the replace function. It still didn't work.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
//&quot;Cookie form values storage&quot;
function Get_Cookie(name) {
var start = document.cookie.indexOf(name+&quot;=&quot;);
var len = start+name.length+1;
if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
if (start == -1) return null;
var end = document.cookie.indexOf(&quot;;&quot;,len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + &quot;=&quot; +escape(value) +
( (expires) ? &quot;;expires=&quot; + expires.toGMTString() : &quot;&quot;) +
( (path) ? &quot;;path=&quot; + path : &quot;&quot;) +
( (domain) ? &quot;;domain=&quot; + domain : &quot;&quot;) +
( (secure) ? &quot;;secure&quot; : &quot;&quot;);
}

function setupForm() {
if (userProfile) getValues(userProfile);
}
function getValues(string) {
getValue(string,&quot;FirstLastName&quot;, document.ptsregb.FirstLastName, &quot;text&quot;);
getValue(string,&quot;EmailAddress&quot;, document.ptsregb.EmailAddress, &quot;text&quot;);
getValue(string,&quot;Program_Title&quot;, document.ptsregb.Program_Title, &quot;select&quot;);
getValue(string,&quot;Location&quot;, document.ptsregb.Location, &quot;text&quot;);
getValue(string,&quot;PhoneNumber&quot;, document.ptsregb.PhoneNumber, &quot;text&quot;);

for (var i=0;i<7+1;i++)
getValue(string,&quot;i&quot;+i, eval(&quot;document.ptsregb.i&quot;+i), &quot;checkbox&quot;);
}
function onCheck(string) { if (string == &quot;on&quot;) return true; return false; }
function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType

var startPos = string.indexOf(elementName + &quot;=&quot;)

if (startPos > -1) {
startPos = startPos + elementName.length + 1;
var endPos = string.indexOf(&quot;&&quot;,startPos);
if (endPos == -1) endPos = string.length;

var elementValue = unescape(string.substring(startPos,endPos));

if (elementType == &quot;text&quot;) object.value = elementValue;
if (elementType == &quot;password&quot;) object.value = elementValue;
if (elementType == &quot;select&quot;) object.selectedIndex = elementValue;
if (elementType == &quot;checkbox&quot;) object.checked = onCheck(elementValue);
if (elementType == &quot;radio&quot;) object[elementValue].checked = true;
}
}

//--></SCRIPT>
<title></title>

</HEAD>
<BODY onLoad=&quot;setupForm()&quot; background=&quot;lt%20background.gif&quot; bgproperties=&quot;fixed&quot;>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
//Establishes the Cookie Expiration Date
var today = new Date();
var expires = new Date(today.getTime() + (365 * 24 * 60 * 60 * 1000)); // expires one year from now
var userProfile = Get_Cookie(&quot;userProfile&quot;);

if (!userProfile) {
document.write('<P>Welcome,<P>According to your records ');
document.write('you have not set your user profile:');
}
else {
document.write('<P>Welcome back,<P>According to your records ');
document.write('the following settings are held in your profile:');
}
//--></SCRIPT>
[sig][/sig]
 
If you're running ASP, why don't you just do this server-side, and fill in the value=&quot;&quot; area of the form inputs with stored values related to a unique ID from the browser's cookie? Then you can store it all in a database and only save the ID value to the browser cookie. No need for Javascript here. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top