Saturday, September 13, 2008

Adding form results to a file (not database) in coldfusion

How to Add Information to a File
This is a very simple yet very powerful tool to have while using ColdFusion. It comes in very handy when you want to record information but do not want to have to set up a DSN.
What this coding does is simply add results of a form (or any information) to a specified file.
I will first describe use of this file in adding information from a form to the file.
This requires 3 files for use with a form (2 always mandatory).
This is the actual code that adds the information to the file:
Put this code in a .cfm file (logcode.cfm here):

<cffile action="append" file="log.cfm" output=" | | #remote_addr#"addnewline="Yes">

This is the code you need on the form in a separate .cfm file at any part of your website:

<form action="logcode.cfm" method="post">
Name: <input type="text" name="name">
E-Mail: <input type="text" name="email">
<input type="submit" value="Submit">

Last, you need to create the file the log will be in, name it "log.cfm" or whatever you choose so long as it corresponds to what is in the first file. Leave this file blank, as information will be written to it through the form.
Everytime someone inputs that information in you form, it will be automatically added to log.cfm.
I've found this script serves a great part simply as a visit tracker.
All you have to do is use the same code as above and put it anywhere on a page. You can get rid of the form results fields and just have it log the IP address of the visitor, the time of the visit, or any other informational gathering code.

<cffile action="append" file="log.cfm" output="#remote_addr#| DateFormat(now(), 'mmmm dd, yyyy')# #TimeFormat(Now(), 'hh:mm:ss tt')#" addnewline="Yes">
This code will log the page visitor's IP address, the date, and the time of their visit. It makes a very simple way to track usage to your site.
This code comes in great in any page or form. I like using it for user posted comments so I can easily see everything that has been submitted.
Tip: in the "output" field, use table formatting to create a very professional and easy to follow list.

No comments: