csvcoldfusionappendcfmlmailer

ColdFusion take client form submission data and write/append to csv file


I have a relatively basic feedback form on my site which works.

I am trying to build another similar form but instead of using the mailer I just want to store the data in a csv file.

I am taking my existing code and trying to modify it, as well as researching how this might be done (I only know very rudimentary ColdFusion stuff).

There is the whole code with the mailer part that works, and me trying to modify the data to push it to the csv file.

<!---<cfmail to="me@email.com" from="feedback@emailcom" cc="data@email.com" subject="Response">
    <cfloop list="#form.fieldnames#" index="el">
        #el#: #form[el]# #chr(13)# #chr(10)#
    </cfloop>
    URL #CGI.http_referer#
    IP #CGI.remote_addr#
    User Agent #CGI.http_user_agent#
    Date #DateFormat( Now(), "mmm d, yyyy" )# at #TimeFormat( Now(), "hh:mm TT" )#
</cfmail>--->

<cfset headerColumn = "no.,prompt,name,actual_name,email,comments,auth,gcaptcha">
<cfset fileOutputPath = "C:\path\to\data">               
           
<cffile
    action="write"
    file="#fileOutputPath#\submissions.csv" 
    output="#headerColumn#"
    nameconflict="overwrite"
    addnewline="yes"
>
                   
<cfloop query="getSubmissions" list="#form.fieldnames#" index="el">
    <cfset data = #el#,#form[el]#,#chr(13)#,#chr(10)# >
    <!--- <cfset out_cols = "#prompt#,#name#,#actual_name#,#email#,#comments#,#auth#,#gcaptcha#"> --->
    <cffile
        action="append"
        file="#fileOutputPath#\submissions.csv"
        output="#data#"
        addnewline="yes"
    >
</cfloop> 

The nice thing about this:

<cfloop list="#form.fieldnames#" index="el">
     #el#: #form[el]# #chr(13)# #chr(10)#
</cfloop>

Is I guess this works just looping through the form fields, presumably it doesn't matter if there are 1 or 100.

I dont know if I have to explicitly set each field for the csv file, although this kind of makes sense seeing as I am defining the headers. So something like

form[0] = name;
form[5] = comments;

Currently I get an error, I assume the concatenation is not correct. Anyone know how I go about this?

If you need to see other parts of the application let me know, but the mailer part works I am just trying to adapt it to write to the file.

TIA

EDIT

Here is the error I am seeing:

Column  27
Detail  ColdFusion was looking at the following text:<p>,</p><p>The CFML compiler was processing:<ul><li>A cfset tag beginning on line 23, column 10.<li>A cfset tag beginning on line 23, column 10.</ul>
KnownColumn 26
KnownLine   23
KnownText   #
Line    23
Message Invalid CFML construct found on line 23 at column 27.
Snippet <cfset data = #el#,
StackTrace  coldfusion.compiler.ParseException: Invalid CFML construct found on line 23 at column 27.

Solution

  • <cfset data = #el#,#form[el]#,#chr(13)#,#chr(10)# >
    

    This line needs quotes. With the # on the vars it will output the right data as well as keeping standard string values.

    <cfset data = "#el#,#form[el]#,#chr(13)#,#chr(10)#">