ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old August 5th, 2003, 02:43 PM
BicyclePunk BicyclePunk is offline
Indie Poser Punk
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Wayne, PA
Posts: 199 BicyclePunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 29 sec
Reputation Power: 8
Simple Write to File

Ok, I am completely new to ColdFusion. Completely. But I am on a small project right now where I have a form on a web page and I need to write the results of that form to a flat text file...the file can be comma delimited or tab...doesn't matter.

I have the ColdFusion MX Bible, but I can't seem to find a quick answer. I can do this in seconds with PHP...but this is all news to me.

Can someone point me to an easy tutorial or show me some code to get me on my way? Thanks.

Reply With Quote
  #2  
Old August 5th, 2003, 07:15 PM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,488 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 10 m 11 sec
Reputation Power: 42
You can do this with one line of code using the <cffile> tag.

Reply With Quote
  #3  
Old August 6th, 2003, 08:31 AM
BicyclePunk BicyclePunk is offline
Indie Poser Punk
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Wayne, PA
Posts: 199 BicyclePunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 29 sec
Reputation Power: 8
So no other ColdFusion code on the page? I assume I have to save the file as a .cfm page, correct? Thanks.

Reply With Quote
  #4  
Old August 6th, 2003, 12:42 PM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,488 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 10 m 11 sec
Reputation Power: 42
No you can write out the file with any file extension you want, but it will basically be a text file.

<cffile action="WRITE" file="c:\myfile.txt" output="What I want to write to the file. This can be a variable, of course.">

Reply With Quote
  #5  
Old August 6th, 2003, 01:43 PM
BicyclePunk BicyclePunk is offline
Indie Poser Punk
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Wayne, PA
Posts: 199 BicyclePunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 29 sec
Reputation Power: 8
Actually, I meant save the page with the form as a cfm page. And the c:\ reference? Is that the same as directories within my web server? One last thing....variables are noted as #variable#, right? Thanks a ton for all this help!!!!

Reply With Quote
  #6  
Old August 6th, 2003, 05:03 PM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,488 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 10 m 11 sec
Reputation Power: 42
Maybe I'm missing something. Lets break it down:

myformpage.cfm has HTML and form elements in it...text boxes, select boxes, etc. The action of the form points to myformpage_action.cfm or something. (the names can obviously be anything you need them to be).

The user submits the form which issues an HTTP POST to the myformpage_action.cfm page.

On myformpage_action.cfm, all of the values that the user entered into the form are available in the FORM scope. So if myformpage.cfm had a form field called "username", the myformpage_action.cfm could reference that as #form.username#.

Now you could do this:

<cffile action="WRITE" file="c:\myformdata.txt" output="The user name is #form.username#.">

The path to the file can be whatever you need it to be, but yes, it is a path on the web server. CF (nor any other app server) has access to save files to the client's hard drive.

You can also use action="APPEND" to append the data to an existing text file if you don't want to overwrite it.

Reply With Quote
  #7  
Old August 7th, 2003, 12:34 PM
BicyclePunk BicyclePunk is offline
Indie Poser Punk
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Wayne, PA
Posts: 199 BicyclePunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 29 sec
Reputation Power: 8
It's not you that's missing something, it's me....I'm a complete CF newbie. here is what I have for the form on this page:

PHP Code:
<cffile action="WRITE" file="/myformdata.txt" output="#form.username#" action="APPEND">  
            <
form action="elertBGthanks.cfm" method="post" name="form">
              <
table border="0" cellspacing="3" cellpadding="3">
                <
tr
                  <
td align="right" valign="top">User Name</td>
                  <
td align="left" valign="top"><input name="username" type="text" id="username"></td>
                </
tr>
                <
tr
                  <
td align="right" valign="top">&nbsp;</td>
                  <
td align="left" valign="top"><table width="100%" border="0" cellspacing="3" cellpadding="3">
                      <
tr align="left"
                        <
td align="right"> <input type="submit" name="Submit" value="Submit"
                        </
td>
                        <
td> <input name="Reset" type="reset" id="Reset" value="Reset"></td>
                      </
tr>
                    </
table></td>
                </
tr>
              </
table>
              </
form


basically nothing is working. I hit submit and I get a page cannot be displayed error for elertBGthanks.cfm. And I have no file on my web server called myformdata.txt. Thanks again for the advice.....

Reply With Quote
  #8  
Old August 11th, 2003, 09:02 AM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,488 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 10 m 11 sec
Reputation Power: 42
I think that the problem is that the CFFILE tag (which does the write of the form data to the file) has to be on the ACTION page. Where you have the CFFILE tag now, there is no form data yet. Only when the submit the form is the form data available. Try moving the CFFILE tag to the action page (elertBGthanks.cfm).

Reply With Quote
  #9  
Old August 11th, 2003, 10:21 PM
BicyclePunk BicyclePunk is offline
Indie Poser Punk
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Wayne, PA
Posts: 199 BicyclePunk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 29 sec
Reputation Power: 8
Man...I just can't get it to work. I can get the thanks page to display properly when I type in the URL, but when I get to it from the first page, it won't work. Thanks for the help, but I'm going to see if I can get this person to move the site to a Linux machine so I can use PHP. Thanks, though!

Reply With Quote
  #10  
Old August 15th, 2003, 05:46 AM
Wrath Wrath is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Johannesburg, South Africa
Posts: 4 Wrath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This is a quick and dirty solution that will work with any form. As long as the code below (that resided on the action page) posts the dat using a HTTP POST method this should work file.

<!--- Check fi FIELDNAMES is defined (CF passes along list of fieldnames with all form POSTS) --->
<CFIF IsDefined("FIELDNAMES")>

<!--- Set empty string to hold file content --->
<CFSET Str = "">

<!--- Loop over form fields and build string to write to file, each item seperated by CR + LF --->
<CFLOOP INDEX="Index" LIST="#FIELDNAMES#">
<CFSET STr = Str & Chr(13) & Chr(10) & Index & " : " & Evaluate(Index)>
</CFLOOP>

<!--- replace first CR + LF with nothing (remove) --->
<CFSET Str = Replace(Str, Chr(13) & Chr(10), "", "ONE")>

<!--- write content of STR to file --->
<CFFILE ACTION="WRITE" FILE="text01.txt" OUTPUT="#STR#" ADDNEWLINE="NO">

</CFIF>

OR

<!--- Check fi FIELDNAMES is defined (CF passes along list of fieldnames with all form POSTS) --->
<CFIF IsDefined("FIELDNAMES")>

<!--- Loop over form fields and build string to write to file, each item seperated by CR + LF --->
<CFLOOP INDEX="Index" LIST="#FIELDNAMES#">
<CFFILE ACTION="APPEND" FILE="text02.txt" OUTPUT="#Index# : #Evaluate(index)#" ADDNEWLINE="YES">
</CFLOOP>
</CFIF>

Reply With Quote
  #11  
Old April 19th, 2004, 03:40 PM
PlazMan PlazMan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 PlazMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi.
I am trying to do the same thing... BUT, I want the end result to be a comma delimited file with quotation marks as the text qualifier.... Like:

"FirstName","LastName","Date"
"Geddy","Lee","04-04-07"
"Neal","Peart","04-04-08"
"Alex","Lifeson","04-04-09"

Can you tell me how to do that? I am close, but I cannot get quotation marks in my output, and also the "AddNewLine=Yes" seems to do nothing at all.

Reply With Quote
  #12  
Old April 19th, 2004, 03:54 PM
chrisgreen chrisgreen is offline
Webmaster
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 23 chrisgreen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Append the output attribute to something like the following:-

<CFFILE ACTION="APPEND" FILE="text02.txt" OUTPUT=" '#form.username#','#form.password#' " ADDNEWLINE="YES">

Reply With Quote
  #13  
Old April 19th, 2004, 04:07 PM
PlazMan PlazMan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 PlazMan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Will that place quotes around each?

Reply With Quote
  #14  
Old April 19th, 2004, 04:18 PM
chrisgreen chrisgreen is offline
Webmaster
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 23 chrisgreen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes, it should.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Simple Write to File


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump