Electric Type

Multimedia

About Us

News

Help

Cool Forms with ColdFusion

Page 6 — Memory with Cookies

Do you want your form to remember information from visit to visit? The easiest way to do this is by using cookies. Whenever a user submits a form, we can write all of the form values to a cookie. Then the next time the user comes to the site, we can check to see if that cookie exists. If it does, we can use his or her existing data to set up a form.

First, we have to store the information. Since we don't want to store a bunch of junk, we need to wait until the form has passed validation before setting the cookie. Open a blank document, paste in this code, then save it as process.cfm.

<cfcookie name="forminfo" value="#form.name#, #form.address#,
#form.city#, #form.state#, #form.zip#, #form.phone#" expires="NEVER">

Whenever a form has been successfully submitted, this line of code will store the user's personal information in a cookie called forminfo. Since the expiration date is set to "never," the information will be stored forever. This means that if the user ever visits the site again, his or her data is accessible in the form of a cookie.

Now that we have a cookie, we need to access it. In order to do this, we need to do two more things. First, we have to check to see whether the cookie exists. If it does, we need to set all of the form values equal to the values stored in the cookie. Open another blank document and paste the following code into it:

<cfif parameterexists(cookie.forminfo)>	<cfif listlen(cookie.forminfo) eq 6>		<cfscript>		name = listgetat(#cookie.forminfo#, 1);		address = listgetat(#cookie.forminfo#, 2);		city = listgetat(#cookie.forminfo#, 3);		state = listgetat(#cookie.forminfo#, 4);		zip = listgetat(#cookie.forminfo#, 5);		phone = listgetat(#cookie.forminfo#, 6);		</cfscript>	</cfif></cfif>

This is the code that will check for the cookie and set the form values if the cookie exists. Save this document as cookieset.cfm. Open your form.cfm file and include the cookieset file right above the input file so the page looks like this:

<cfif form.step eq "process">	<cfinclude template="validate.cfm"><cfelseif form.step eq "input">	<cfinclude template="cookieset.cfm">	<cfinclude template="input.cfm"></cfif>

Congratulations! Your form now has long- and short-term memory.

next page»


Tutorials  

User Blogs  

Teaching Tools  

Authoring  

Design  

Programming help  

Advanced Flash  

Javascript  

Glossary  

PHP Coding  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.