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

Need the assistance of a Javascript Pro 1

Status
Not open for further replies.

nalbiso

Programmer
Aug 31, 2000
71
US
Hi

This is an old question but I never received an answer that worked for my situation, or I just didn't understand the answer correctly. I am very new at javascript and I am attempting to write a cookie for a form that includes a visitor's name, address, phone number, email address so that each time the visitor returns to the form, this information is automatically filled out with the information left from before.

I tried this code once and it worked, but then I changed the expiration and it never worked again.

I believe that there is something wrong with the Get_Cookie(Name) and the Set_Cookie parts but I am very new to this and I haven't received an explanation as to why it's wrong and how to fix it

Here's the code:

<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>


Please help! This problem has and continues to plague me.

Thanks! :) [sig][/sig]
 
Hi,

I think that I might have been the one that responded to your original post.

Instead of addressing the problems with your script above, I've put together a sample script with form that provides the basic functionality that you refer to in the first paragraph of your post:

A page that remembers values for a given form with cookies and fills in these values automatically when the user returns to the site. I've included some comments that will (hopefully) be helpful.

Good luck!

<script language=&quot;javascript&quot;>
<!--

// Here you set the expiration date for each cookie you set. This will be passed to the
// Set_Cookie() function each time one of the form values is changed.
var expires=new Date();
expires.setTime (expires.getTime() + 24 *60 *60 *30 * 1000);

// I think I mentioned in my previous response to you that your Get_Cookie() function
// has a problem. It works for most cases, but will fail if you have, for example,
// two different cookie names that contain the same set of characters at the end. In
// other words, if you are looking for a cookie named &quot;me&quot; but ALSO have a cookie named
// &quot;name&quot; in the same set of cookies that occurs BEFORE &quot;me&quot; in the cookie string, a request
// for the &quot;me&quot; cookie will return the value for the &quot;name&quot; cookie. Try it out and you'll see
// what I mean and examine the code so you can see why this is so.
//
// Below is my own cookie retrieval function that doesn't have this problem:
function get_cookie(myname)
{
var start;
var end;
var last_cookie=false;
var wc;

for (start=0;!last_cookie;start=end+2) {
if ((end=document.cookie.indexOf(&quot;;&quot;,start))==-1) {
end=document.cookie.length;
last_cookie=true;
}
wc=unescape(document.cookie.substring(start,end));
if (wc.substring(0,wc.indexOf(&quot;=&quot;))==myname) {
return wc.substring(wc.indexOf(&quot;=&quot;)+1);
}

}
return &quot;&quot;;
}

// No problem here as long as you pass 'expires' the value that it wants (which
// we've taken care of above)
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;);
}

// This function fills in the form with cookie values for the corresponding form elements
// if the cookies exist.
function fill_in_form(form_name)
{
var tmp;

// loop through each of the form elements
for (var i=0;i<document.forms[form_name].elements.length;++i) {
// This limits searches to just
// text boxes, this would obviously have to be changed if you wanted
// to include other form elements
if (document.forms[form_name].elements.type==&quot;text&quot;) {
// does the cookie exist for this element?
if ((tmp=get_cookie(document.forms[form_name].elements.name))!=&quot;&quot;) {
// Yes, put it in the form
document.forms[form_name].elements.value=tmp;
}
}
}
}

//-->
</script>

<!-- Fill in the form with any cookies that might exist from the previous visit -->

<body onLoad=&quot;fill_in_form('user_info');&quot;>

<!--
Each time one of the text elements loses focus the cookie for that
element is set again. You also might want to run each of the values through
a validation script (e.g. check if the e-mail field is a valid e-mail address)
before setting the cookie.
-->

<form name=&quot;user_info&quot;>
<pre>
name: <input type=&quot;text&quot; name=&quot;name&quot; onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;>
address: <input type=&quot;text&quot; name=&quot;address&quot; onBlur=&quot;Set_Cookie('address',this.value,expires,null,null,null);&quot;>
phone: <input type=&quot;text&quot; name=&quot;phone&quot; onBlur=&quot;Set_Cookie('phone',this.value,expires,null,null,null);&quot;>
e-mail: <input type=&quot;text&quot; name=&quot;email&quot; onBlur=&quot;Set_Cookie('email',this.value,expires,null,null,null);&quot;>
</pre>
</form>
</body> [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
Hi Russ,

If you're still out there, I am still having problems, but I think that I am half way there.

I was testing your code out on another webform that I have in a different website so I could see if it writes the cookie, which it was not doing before (I am assuming it was because of the name issue). I looked in my cookie file and it is writing there when I fill out the form.

But two things are happening. One, it is only taking the values from a radio button that I have on the form and I want it to take info from the radio button and the text boxes on the form. Two, the information is still not appearing when I reload the page which means that it is either not reading the correct cookie or I confused it somehow.
What else did I do wrong here?
_______________________________________________________________________
<script language=&quot;javascript&quot;><!--
var expires=new Date();
expires.setTime(expires.getTime()+24*60*60*30*1000);
function get_cookie(jobint)
{
var start;
var end;
var last_cookie=false;
var wc;
for (start=0;!last_cookie;start=end+2){
if((end=document.cookie.indexOf(&quot;;&quot;,start))==-1){
end=document.cookie.length;
last_cookie=true;
}

}
return&quot;&quot;;
}
// function fills out the form with the cookie values for the corresponding
// form elements

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;) +
( (secure) ? &quot;;secure&quot; : &quot;&quot;);
}
function fill_in_form(jobint)
{
var tmp;
// loop through each of the form elements
for (var i=0;i<document.forms[jobint].elements.length;++i){
if (document.forms[jobint].elements.type==&quot;text&quot;){
if (document.forms[jobint].elements.type==&quot;radio&quot;){
//does the cookie exist for this element?
if
((tmp=get_cookie(document.forms[jobint].elements.name))!=''){
//if yes, then put it in the form
document.forms[jobint].elements.value=tmp;
}
}
}
}
}
//-->
</script>
<!-- Fill in the form with any cookies that might exist from the previous
visit -->

//This form was done with Frontpage2000 and I used the built in features for form field validation, so I put the onblur statements here.

<td width=&quot;199&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Your First Name:
</font><font face=&quot;Tahoma&quot; color=&quot;#FF0000&quot;>*R</font></td>
<td width=&quot;263&quot; colspan=&quot;6&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Your Last Name:</font><font face=&quot;Tahoma&quot; color=&quot;#FF0000&quot;>*R</font></td>
</tr>
<tr>
<td width=&quot;199&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;><!--webbot
bot=&quot;Validation&quot; S-Display-Name=&quot;first name &quot;
B-Value-Required=&quot;TRUE&quot; I-Minimum-Length=&quot;1&quot;
I-Maximum-Length=&quot;40&quot; --><input type=&quot;text&quot; name=&quot;First_Name&quot; size=&quot;22&quot; maxlength=&quot;40&quot; tabindex=&quot;0&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
<td width=&quot;263&quot; colspan=&quot;6&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;><!--webbot
bot=&quot;Validation&quot; S-Display-Name=&quot;last name &quot;
B-Value-Required=&quot;TRUE&quot; I-Minimum-Length=&quot;1&quot;
I-Maximum-Length=&quot;40&quot; --><input type=&quot;text&quot; name=&quot;Last_Name&quot; size=&quot;16&quot; maxlength=&quot;40&quot; tabindex=&quot;1&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
</tr>
<tr>
<td width=&quot;199&quot;> <font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> Street Address:</font></td>
<td width=&quot;97&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>City:</font></td>
<td width=&quot;61&quot; colspan=&quot;3&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> State:</font></td>
<td width=&quot;69&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Zip Code:</font></td>
<td width=&quot;18&quot;></td>
</tr>
<tr>
<td width=&quot;199&quot;> <font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> <input type=&quot;text&quot; name=&quot;Address&quot; size=&quot;22&quot; tabindex=&quot;2&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
<td width=&quot;97&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> <input type=&quot;text&quot; name=&quot;City&quot; size=&quot;13&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;> </font></td>
<td width=&quot;61&quot; colspan=&quot;3&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> <input type=&quot;text&quot; name=&quot;State&quot; size=&quot;2&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
<td width=&quot;69&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;><input type=&quot;text&quot; name=&quot;Zip_Code&quot; size=&quot;5&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
<td width=&quot;18&quot;></td>
</tr>
<tr>
<td width=&quot;199&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Exact Title of Position:
</font><font face=&quot;Tahoma&quot; color=&quot;#FF0000&quot;>*R</font></td>
<td width=&quot;82&quot; colspan=&quot;2&quot;>
<font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>
Job Code: </font>
<font face=&quot;Tahoma&quot; color=&quot;#FF0000&quot;>
*R</font></td>
<td width=&quot;298&quot; colspan=&quot;4&quot; rowspan=&quot;2&quot;>
</td>
</tr>
<tr>
<td width=&quot;199&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;><!--webbot
bot=&quot;Validation&quot;
S-Display-Name=&quot;title of position expressing interest in &quot;
B-Value-Required=&quot;TRUE&quot; I-Minimum-Length=&quot;1&quot;
I-Maximum-Length=&quot;60&quot; --><input type=&quot;text&quot; name=&quot;Title_of_Position&quot; size=&quot;22&quot; maxlength=&quot;60&quot; tabindex=&quot;3&quot;>
</font></td>
<td width=&quot;56&quot; colspan=&quot;2&quot;>
<font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>
<!--webbot bot=&quot;Validation&quot; S-Display-Name=&quot;job code&quot;
S-Data-Type=&quot;Number&quot; S-Number-Separators=&quot;x.&quot; B-Value-Required=&quot;TRUE&quot;
I-Minimum-Length=&quot;4&quot; I-Maximum-Length=&quot;4&quot; --><input type=&quot;text&quot; name=&quot;Job_Code&quot; size=&quot;4&quot; tabindex=&quot;4&quot; maxlength=&quot;4&quot;></font></td>
</tr>
<tr>
<td width=&quot;454&quot; colspan=&quot;7&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Are you available to substitute in this position?</font></td>
</tr>
<tr>
<td width=&quot;184&quot; colspan=&quot;4&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> Yes <input type=&quot;radio&quot; value=&quot;Yes&quot; checked name=&quot;Available_to_Substitute&quot; tabindex=&quot;5&quot;>
No<input type=&quot;radio&quot; name=&quot;Available_to_Substitute&quot; value=&quot;No&quot; tabindex=&quot;6&quot;></font></td>
<td width=&quot;264&quot; colspan=&quot;3&quot;></td>
</tr>
<tr>
<td width=&quot;184&quot; colspan=&quot;4&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Your Phone Number:</font></td>
<td width=&quot;264&quot; colspan=&quot;3&quot;></td>
</tr>
<tr>
<td width=&quot;184&quot; colspan=&quot;4&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> <input type=&quot;text&quot; name=&quot;areacode&quot; size=&quot;2&quot; value=&quot;909&quot; maxlength=&quot;3&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;>
<!--webbot bot=&quot;Validation&quot; S-Data-Type=&quot;Integer&quot;
S-Number-Separators=&quot;x&quot; --><input type=&quot;text&quot; name=&quot;Phone_Number1&quot; size=&quot;2&quot; maxlength=&quot;3&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;>-<!--webbot
bot=&quot;Validation&quot; S-Data-Type=&quot;Integer&quot; S-Number-Separators=&quot;x&quot;
--><input type=&quot;text&quot; name=&quot;Phone_Number&quot; size=&quot;4&quot; tabindex=&quot;7&quot; maxlength=&quot;4&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
<td width=&quot;264&quot; colspan=&quot;3&quot;></td>
</tr>
<tr>
<td width=&quot;184&quot; colspan=&quot;4&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Your email address:</font></td>
<td width=&quot;264&quot; colspan=&quot;3&quot;></td>
</tr>
<tr>
<td width=&quot;184&quot; colspan=&quot;4&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;> <input type=&quot;text&quot; name=&quot;Email_Address&quot; size=&quot;19&quot; tabindex=&quot;8&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
<td width=&quot;264&quot; colspan=&quot;3&quot;></td>
</tr>
<tr>
<td width=&quot;454&quot; colspan=&quot;7&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Which method do you prefer to be contacted by:&nbsp;</font></td>
</tr>
<tr>
<td width=&quot;454&quot; colspan=&quot;7&quot;><font face=&quot;Tahoma&quot; color=&quot;#000080&quot;>Phone <input type=&quot;radio&quot; name=&quot;Preferred_Method_of_Contact&quot; value=&quot;Phone&quot; tabindex=&quot;9&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot; checked>
Email <input type=&quot;radio&quot; name=&quot;Preferred_Method_of_Contact&quot; value=&quot;Email&quot; tabindex=&quot;10&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;>
Regular Mail <input type=&quot;radio&quot; name=&quot;Preferred_Method_of_Contact&quot; value=&quot;Regular Mail&quot; tabindex=&quot;11&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;>
All contact methods <input type=&quot;radio&quot; name=&quot;Preferred_Method_of_Contact&quot; value=&quot;All Contact Methods&quot; tabindex=&quot;12&quot;
onBlur=&quot;Set_Cookie('name',this.value,expires,null,null,null);&quot;></font></td>
</tr>
</table>
<p><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;></p>
</form>


_______________________________________________________________________

Thanks for your help!

[sig][/sig]
 
Your first problem is that removed the most important part from your get_cookie() function. Compare the one I posted to yours and you'll see what I mean. Yours will always return &quot;&quot;.

Your second problem is that in your fill_in_function() you first test for type &quot;text&quot; and then you test the same element for type &quot;radio.&quot; Therefore, no element will pass the test as no element can be both &quot;text&quot; AND &quot;radio.&quot; The comparison and assignments should be something like:

if (document.forms[form_name].elements.type==&quot;text&quot; ||
document.forms[form_name].elements.type==&quot;radio&quot;) {
// does the cookie exist for this element?
if ((tmp=get_cookie(document.forms[form_name].elements.name))!=&quot;&quot;) {
if (document.forms[form_name].elements.type==&quot;radio&quot;) {
if (document.forms[form_name].elements.value==tmp) {
document.forms[form_name].elements.checked=true;
}
} else {
document.forms[form_name].elements.value=tmp;
}
}
}

The other problem I saw was that each of your calls to Set_Cookie() overwrites the previous one, due to the fact that you use the same value for the name of the cookie. Just code each of your Set_Cookie() calls like this:

Set_Cookie(this.name,this.value,expires,null,null,null);

There may be other problems, but try this out and see if it works. [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
Hi Russ.

I tried what you said, but I still get the following errors.

JavaScript Error: line 40:

syntax error.

if (document.forms[jobint].elements.type==&quot;text&quot;)||
............................................................^

JavaScript Error: line 65:

fill_in_form is not defined.
JavaScript Error: line 46:

syntax error.

if (document.forms[jobint].elements.type==&quot;radio&quot;){
........^

JavaScript Error: line 70:

fill_in_form is not defined.

I'll keep working on it, but let me know if you have any ideas.

Thanks

[sig][/sig]
 
if (document.forms[jobint].elements.type==&quot;text&quot;)||

should be:

if (document.forms[jobint].elements.type==&quot;text&quot; ||

** given that 'i' is your element counter

if (document.forms[jobint].elements.type==&quot;radio&quot;){

again, you need the index for the element:

if (document.forms[jobint].elements.type==&quot;radio&quot;){

check to make sure that 'fill_in_form()' is the name of the function you're calling and that you're calling it by its EXACT name. [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top