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="#form.name# | #form.email# | #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">
</form>

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.

About This Tutorial
Author: Mike Bamberger
Skill Level: Beginner 
 
 
 
Platforms Tested: CFMX
Total Views: 36,515
Submission Date: July 03, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • This is still a bit confusing, I've tried it several ways and it's not working. the cffile coding goes in it's own file, is that correct? The form coding referencing the logcode.cfm, which file does that go in? I have a signup.cfm for members joining at my sight, and a signupaction.cfm that goes with that. Does that form coding go in singup.cfm or signupaction.cfm? Thanks.

  • Code in Flash for Submit button on (release) { if (username.text == "" || email.text == "" || wedding.text == "" || comments.text == "") { getURL("javascript:alert('name, email, wedding date, and comments are needed');"); gotoAndStop("feedbackform"); } else { cfData = new LoadVars(); cfData.onLoad = function() { _root.gotoAndStop ("results"); }; cfData.username = _root.username.text; cfData.email = _root.email.text; cfData.weddingdate = _root.wedding.text; cfData.comments = _root.comments.text; cfData.sendAndLoad("sendfeedback.cfm", cfData, "POST"); _root.gotoAndStop("wait"); } } Code for sendfeedback.cfm sendfeedback.cfm #form.username# #form.email# #form.wedding# #form.comments#

  • I was wondering if you can tell me how to output the information into a table...and also if you can make a thank you screen come up after the form is submitted instead of a blank screen. Thank you for any information you can provide.

  • Can this code be used to log to a .txt file? I have tried and am getting an error saying that all or part of the path is incorrect. I am using the full path and have checked and re-checked.

  • I just tried your code. I replaced \\servername\folder1\folder2\folder3\Test\log.csv with my own path. It worked fine. This leads me to believe that folder1 is not a share on servername or that you do not have write permissions to the Test folder. Hope this helps.

  • Hello Guys, this is really helpful, except, when I try this I get an error message : Error: The file could not be accessed. Now here is the form.cfm : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Form

    Name:
    NT Login:
    Siebel Login:
    Axys Login:
    Submit Request :
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ And here is the Action file (action.cfm): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Processing Done. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Can someone please tell me why this would not work? Or what do I need to do? I made sure that the folder and file has read/write access. Thank you for your help. :)

  • zxc

  • I'm looking for an example on how to do this that is more robust. Something that will take care of the issue of two people trying to access the file at the same time. I think cflock will do it for me. I'm a beginner and I'm not sure how I would use cftry/cfcatch with cflock.

  • A number have people have e-mailed me saying the information isn't getting written to the file. The line: file="log.cfm" is the problem. That needs to be the physical path on the server. For example, c:\websites\yoursite.com\wwwroot\log.cfm Change that and it will work. :)

  • I copied and pasted everything exactly as you presented it. Nothing is getting written in the log.cfm file. What am I doing wrong?

Advertisement

Sponsored By...
Powered By...