How to write information to a file from a form in coldfusion
I was wondering if anyone knows how to use ColdFusion to write information to a file that has been input into a form.
I am not talking about a database, as I know how to do this already. But I am interested in writing form data into a .txt or .log file. I have tried many things but cannot get it to work correctly.
If anyone could help me this would be great, my e-mail is admin@sikweb.com or you can post a reply here.
Thanks a lot
Comments
I figured it out
I spent this morning trying things out and my mistake was simple, i was using an "write" command instead of "append." So, below is the coding to perform this actually simple task. Hope it comes in handy.
This requires 3 files (if you want to use it with a form)
This is the actual code that adds to the file:
Put this code in a .cfm file (logcode.cfm here):
<cffile action="append" file="log.cfm" output="#form.input#" addnewline="Yes">
I think you may have to put the file in its root location on the server, but I'm not sure. I would do it just to be safe (ie: Z:/websites/domain.com/logs/log.cfm")
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="input"><p>
<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.
Everytime someone inputs that information in you form, it will be automatically added to log.cfm. I found it makes an easy visitor logging tool by replacing it as a form and putting it simply as a code on a page, where instead of #form.input# I use #remote_addr# to gather the IP.
Thanks!
You must me logged in to write a comment.