I have built what I think is a nice contact form.

I want to add a cookie to this form so that if a visitor has already completed this form, the browesr recognizes the user and sends them to a page that states they have already filled out this form.

It is my understanding that using a cookie is the only way to accomplish this and I really do not understand how to use cookies. Shame on me I guess.

Any assistance is greatly appreciated.

1.	<html>
2.	<head>
3.	 
4.	<title>Contact Form</title>
5.	 
6.	 
7.	<script language="JavaScript" type="text/javascript"> 
8.	 
9.	function checkForm() {
10.	 document.form.Email.value = " " + document.form.Email.value;
11.	 
12.	  if (document.form.FName.value == "") {alert('The First Name field is empty!'); return false; }  // script to validate First Name field   
13.	 
14.	 if (document.form.LName.value == "") {alert('The Last Name field is empty!'); return false; }  // script to validate Last Name field   
15.	 
16.	  if (document.form.Address.value == "") {alert('The Address field is empty!'); return false; }  // script to validate Address field
17.	 
18.	  if (document.form.City.value == "") {alert('The City field is empty!'); return false; }  // script to validate City field
19.	 
20.	  if (document.form.State.selectedIndex == " ") {alert('You have not selected a state!'); return false; }  // script to validate State field
21.	 
22.	  if (document.form.Zip.value == "") {alert('The Zip Code field is empty!'); return false; }  // script to validate Zipfield
23.	 
24.	 
25.	 }
26.	 
27.	// -->
28.	</script>
29.	 
30.	 
31.	<script language=”JavaScript” type=”text/javascript”> 
32.	document.cookie = "UserName=Paul;expires=Tue, 28 Dec 2020 00:00:00 GMT;path=/;";
33.	</script>
34.	 
35.	 
36.	</head>
37.	 
38.	<body>
39.	 
40.	 
41.	 <table border="0" cellpadding="0" cellspacing="0" align="center" width="460">
42.	  <tr>
43.	    <td style="background-color:red" align="center" colspan="2">
44.	      <span style="color:black"><b>Contact Form</b></span>
45.	      </font>
46.	    </td>
47.	  </tr>
48.	 
49.	  <form name="form" method="post" action="success.html"
50.	   enctype="text/plain" onSubmit="javascript:return checkForm()";> 
51.	 
52.	  <tr>
53.	    <td width="170" height="30">First Name:</td>
54.	    <td width="290" height="30">
55.	     <input size="20" name="FName" style="font-size:9pt" />
56.	    </td>
57.	  </tr>
58.	 
59.	 
60.	  <tr>
61.	    <td width="170" height="30">Last Name:</td>
62.	    <td width="290" height="30">
63.	     <input size="20" name="LName" style="font-size:9pt" />
64.	    </td>
65.	  </tr>
66.	 
67.	 
68.	  <tr>
69.	    <td width="170" height="30">Address:</td>
70.	    <td width="290">
71.	     <input size="45" name="Address" style="font-size:9pt" />
72.	    </td>
73.	  </tr>
74.	 
75.	  <tr>
76.	    <td width="170" height="30">City:</td>
77.	    <td width="290">
78.	     <input size="30" name="City" style="font-size:9pt" />
79.	    </td>
80.	  </tr>
81.	 
82.	  <tr>
83.	<td width="170" height="30">State:</td>
84.	    <td>
85.	     <select name="State">
86.	      <option selected>Choose...</option>
87.	      <option>Alabama</option>
88.	      <option>Alaska</option>
89.	      <option>Arizona</option>
90.	      <option>Arkansas</option>
91.	      <option>California</option>
92.	      <option>Connecticut</option>
93.	      <option>Delaware</option>
94.	      <option>District of Columbia</option>
95.	      <option>Florida</option>
96.	      <option>Georgia</option>
97.	      <option>Hawaii</option>
98.	      <option>Idaho</option>
99.	      <option>Illinois</option>
100.	      <option>Iowa</option>
101.	      <option>Kansas</option>
102.	      <option>Kentucky</option>
103.	      <option>Louisiana</option>
104.	      <option>Maine</option>
105.	      <option>Maryland</option>
106.	      <option>Massachusetts</option>
107.	      <option>Michigan</option>
108.	      <option>Minniesota</option>
109.	      <option>Mississippi</option>
110.	      <option>Missouri</option>
111.	      <option>Montana</option>
112.	      <option>Nebraska</option>
113.	      <option>Nevada</option>
114.	      <option>New Hampshire</option>
115.	      <option>New Jersey</option>
116.	      <option>New Mexico</option>
117.	      <option>New York</option>
118.	      <option>North Carolina</option>
119.	      <option>North Dakota</option>
120.	      <option>Ohio</option>
121.	      <option>Oklahoma</option>
122.	      <option>Oregan</option>
123.	      <option>Pennsylvania</option>
124.	      <option>Rhode Island</option>
125.	      <option>South carolina</option>
126.	      <option>South Dakota</option>
127.	      <option>Tennessee</option>
128.	      <option>Texas</option>
129.	      <option>Utah</option>
130.	      <option>Vermont</option>
131.	      <option>Virginia</option>
132.	      <option>Washington</option>
133.	      <option>West Virginia</option>
134.	      <option>Wisconsin</option>
135.	      <option>Wyoming</option>
136.	     </select>
137.	    </td>
138.	  </tr>
139.	 
140.	 
141.	  <tr>
142.	    <td width="170" height="30">Zip Code:</td>
143.	    <td width="290">
144.	     <input size="30" name="Zip" style="font-size:9pt" />
145.	    </td>
146.	  </tr>
147.	 
148.	 
149.	  <tr>
150.	    <td width="170" height="30">Email Address:</td>
151.	    <td width="290">
152.	     <input size="30" name="Email" style="font-size:9pt" />
153.	    </td>
154.	  </tr>
155.	 
156.	 
157.	   <tr>
158.	    <td width="170" height="30">Phone Number:</td>
159.	    <td width="290">
160.	     <input size="30" name="Phone" style="font-size:9pt" />
161.	    </td>
162.	  </tr>
163.	 
164.	  <tr>
165.	    <td style="background-color:red" align="center" colspan="2">
166.	    </td>
167.	  </tr>
168.	 
169.	 
170.	  <tr>
171.	    <td colspan="2" align="center" height="30">
172.	      <input type="submit" value="Submit" style="font-size:9pt" />&nbsp;
173.	      <input type="reset" value="Reset" style="font-size:9pt" />&nbsp;
174.	    </td>
175.	  </tr>
176.	  </tr>
177.	 
178.	  </form>
179.	 </table>
180.	 
181.	</body>
182.	</html>

Recommended Answers

All 4 Replies

This was a good tutorial.

I now understand how to prompt a user for a name in a new script box. The cookie also remembers the user. that is all well and good.

However, I am trying to get a cookie to remember a user based on filling in data from my contact form. I need a cookie to remember my user and then push him to another website.

I am not sure that I can get all that from the cookie tutorial.

Simple:

Save another cookie when a particular user has already filled his/her form.
You can create a cookie key by using username. Something like this:

var cookie_FormFilled = username + "_form_filled";

Now when user comes at the page, get its username and build above cookie key. For example username is 'xyz', then the key will be 'xyz_form_filled'. Read the this cookie if you get 'true', then user has already filled his/her form. If the values comes out to be null/undefined then prompt the user with the form. And once form is filled save the cookie with value 'true'.

I am sorry. I appreciate the insigt into this but this is still just greek to me. When I say that I have no experience with cookies, I mean no experience.

What you say probably makes sense to someone that has at least used a cookie before but too me, I just do not get it and the more I try to get my head around the cookie the more confused I am getting. This is frustrating becuase I understand HTML and JavaScript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.