Thursday, September 19, 2024

How to Use Cookies, Part III

This article shows you how to implement cookies to pre-fill in form fields for your visitor.

The demo page at http://willmaster.com/a/6/pl.pl?art70 demonstrates with a feedback form. The code can be used for other forms as well. Note: the demo uses a script that requires Perl version 5 or higher, UNIX or Linux, and sendmail.

The demo page and the related code explain how to do it yourself.

The demonstration is called CGI-JS Cookie Feedback Form.

When your visitor goes to a page with your CGI-JS Cookie Feedback Form or other form that uses the code, JavaScript functions check for specific cookies on your visitor’s hard drive. If found (if your visitor has used the form before with cookies enabled), the appropriate form fields are immediately filled in. If not found, the fields remain blank.

(By the way, the CGI-JS Cookie Feedback Form set of code is designed to help you spam-proof your site by eliminating the need to put your own email address anywhere in the form, not even as a hidden form field.)

When your visitor submits the form, the CGI script emails the form info to you and then sets cookies on the visitor’s hard drive. The cookies contain the content of the form fields as submitted to the CGI script. Then the next time the visitor goes to the page with your CGI-JS Cookie Feedback Form, the content of the cookies will enable the JavaScript to immediately fill in some of the form fields for your visitor.

The content of the form fields submitted to the CGI script undergo a bit of encryption so email addresses will have some cloaking.

The JavaScript and Perl script are well commented with implementation instructions and also notes for changing the form and porting the code to other scripts.

Try the CGI-JS Cookie Feedback Form demonstration at http://willmaster.com/a/6/pl.pl?art70

The special cookie code in the CGI script can be put into scripts that serve other forms, too. And those forms can have the JavaScript code to pre-fill form fields. So long as the form fields have identical names (so the cookie names are identical), one form field can be filled with information previously gathered at another form.

Part I of this series talked about what cookies are and why you might want to use them. Part II showed how to make certain things happen depending on how often the visitor has been at your site (such as displaying a certain message the first visit). Those articles are linked from http://willmaster.com/possibilities/archives

As reference, technical specifications for cookies are at http://www.netscape.com/newsref/std/cookie_spec.html

I hope you’ve enjoyed this series.

“WillMaster Possibilities” ezine
http://willmaster.com/possibilities/
mailto:possibilities@willmaster.com

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles