Cfmail and cftextarea not retaining line breaks - coldfusion

I am working on a site that is having a problem with one of its mail forms. When a user enters text into a CFTEXTAREA, it doesn't retain any of the formatting (pressing 'enter' doesn't result in a line break). Not when its stored as a variable or emailed by a <CFMAIL type="HTML" ..>).
A site admin claims that it used to work fine. Any tips?
<cfprocessingdirective suppresswhitespace="No">
<cfmail to="OMITTED" from="OMITTED" subject="#subject#" type="html">
#message#
</cfmail>
</cfprocessingdirective>
<cfform name="mail" action="./mailinglist.cfm?sendMessage=true"
format="#type#"
height="400">
<cfformgroup type="vertical">
<table width="99%" cellpadding="3" cellspacing="0">
<tr>
<td class="form_label">Subject: </td>
<td class="form_field">
<cfinput class="textbox" required="yes"
message="Please fill out the subject heading."
style="width:200px;"
type="text"
name="subject"
label="Subject:" width="200">
</td>
</tr>
<tr>
<td class="form_label">Message: </td>
<td class="form_field">
<cftextarea class="textbox" required="yes"
id="message"
name="message"
message="Please fill out the message body of the form..."
style="width:300px; height:150px;"
width="350" height="250"
label="Message:"
wraptext="72" ></cftextarea>
</td>
</tr>
<tr>
<td class="form_label"></td>
<td class="form_field" align="left" valign="top">
<cfinput class="textbox" disabled value="yes"
type="checkbox"
name="announcement"
label="Post as Announcement?">
Post as Announcement?
</td>
</tr>
</cfformgroup>

To preserve line breaks from a textarea in an HTML-formatted email, you must replace the line breaks with <br> tags:
reReplace(message, '\n', '<br />', 'ALL')
If you don't have additional HTML-formatted content in your email message, you can simply remove the type="HTML" attribute from the CFMAIL tag, and the text from your text area will appear as it was entered with line breaks intact.

I'd use #imthepitts answer but just to add some additional options you can also use
replace(message, chr(10), '<br />', 'ALL')
or
<cfmail type="html">
<pre>
#message#
</pre>
</cfmail>
Note: Markup, all left aligned intentionally to prevent unwanted indentation in the email.

Try using a rich textarea.
<cftextarea name ="message" richtext="true"
other attributes
>
</cftextarea>

Inside your CFmail, you can use #paragraphformat(message)# and that will keep your line breaks.

Related

extract content from html snippet

I have the following code snippet and I'm looking for a better way to parse out the last name.
<TABLE BORDER="0" class="info" width="560">
<TR>
<TD VALIGN="top"> <B>First Name<B></FONT> </TD>
<TD VALIGN="top"> <INPUT TYPE="text" NAME="First_Name" SIZE="16" value="Ashley"> </TD>
<TD VALIGN="top"> <B>Last Name<B></FONT> </TD>
<TD VALIGN="top"> <INPUT TYPE="text" NAME="Last_Name" SIZE="16" value="Smith"> </TD>
</TR>
<tr>
<TD VALIGN="top" colspan="2"> <B>Company Name (if any):<B></FONT> </TD>
<TD VALIGN="top" colspan="2"> <INPUT TYPE="text" NAME="Company_Name" SIZE="24" value=""> </TD>
</tr>
<TR>
<TD VALIGN="top" colspan=2> <B>Address<B></FONT> </TD>
<TD VALIGN="top" colspan=2> <INPUT TYPE="text" NAME="Address" SIZE="24" value="123 Any Street Circle "> </TD>
</TR>
<tr>
<TD VALIGN="top" colspan=2> <B>City <B></FONT> <INPUT type="text" id="City" name="City" SIZE="14" value="Shady Town"> </TD>
<TD colspan="2" VALIGN="top"> <B>State<B></FONT> <INPUT type="text" id=State name=State SIZE="4" value="Tx"> <B>Zip<B></FONT> <INPUT type="text" id=Zip name=Zip SIZE="8
I have the following but I'm pretty sure I can do this without having to do the replace. What I'm trying to do below is find the starting point, finding the end point, and then taking the text in between. Then once I have that, remove the "matched" text leaving me with the value of the input field.
<cfset LastName_start = findNoCase('<INPUT TYPE="text" NAME="Last_Name" SIZE="16" value="', theString, 0)>
#lastName_start#
<cfset LastName_end = findNoCase('">', theString, 0)> #lastName_end#
<cfset lastNameValue = '#Mid(theString,LastName_start,LastName_end)#'>
#lastNameValue#
<cfset lastNameValue = replace(lastNameValue, '<INPUT TYPE="text" NAME="Last_Name" SIZE="16" value="', '')>
<cfset lastNameValue = replace(lastNameValue, '">', '')>
<cfset lastNameValue = listFirst(lastNameValue,'"')>
<cfdump var="#lastNameValue#" label="lastNameValue">
Any tips on how I can clean this up using ColdFusion? This is an ethical exercise.
And yes, I did try to format this.
I second Scott Stroz's suggestion about trying JSoup. It usually works well, and is very simple to use.
Download the JSoup jar and load it in your Application.cfc.
component {
this.name = "MyApplication";
this.javaSettings = { loadPaths = ["C:\path\to\jsoup-1.12.1.jar"] };
// ... more application settings
}
Create an instance of JSoup, parse the HTML string and use val() to grab the text of the first matching element. It returns an empty string if the element wasn't found.
You can find a bunch of other helpful examples in the JSoup Cookbook.
<cfscript>
yourHTMLString = '<TABLE BORDER="0" class="info" ......';
// parse html
jsoup = createObject("java", "org.jsoup.Jsoup");
root = jsoup.parse( yourHTMLString );
// get the first matching value ...
lastName = root.select("input[name='Last_Name']").val();
firstName = root.select("input[name='First_Name']").val();
companyName = root.select("input[name='Company_Name']").val();
cityName = root.select("input[name='City']").val();
stateName = root.select("input[name='State']").val();
address = root.select("input[name='Address']").val();
</cfscript>
Results:

I printed {{comment.text}} in the text area. But there is a space in front. How do I remove blanks?

I printed {{comment.text}} in the text area:
<tr>
<td>code</td>
<td colspan="3" id="my_code_{{comment.id}}">
<textarea name="name" rows="10" cols="80">
{{comment.text}}
</textarea>
</td>
</tr>
But there is a space in front.
How do I remove blanks?
ex)
output of django templates
You have space between textarea. Change your code as shown below:
<textarea name="name" rows="10" cols="80">{{comment.text}}</textarea>
Remove that space infront of {{comment.text}}

CFINPUT required="yes" not working with CFLOOP

When I click Submit button (no data was entered) the text boxes were empty and it still goes to the next page without popping up any messsage. It seems the cfinput required="yes" does not work the cfloop. I thought it should work even with the cfloop. I could not find what was wrong. How can I make it work? any info is greatly appreciated. Thank you.
<cfform name="theForm" action="nextPage.cfm" method="post">
<table>
<tr>
<td><cfinput type="text" name="A" size="50" required="yes message="please enter your text"></td>
</tr>
<td><cfinput type="text" name="B" size="50" required="yes message="please enter your text"></td>
</tr>
<cfloop from=1 to=5 index=i>
</tr>
<td><cfinput type="text" name="C" size="50" required="yes message="please enter your text"></td>
</tr>
</tr>
<td><cfinput type="text" name="D" size="50" required="yes message="please enter your text"></td>
</tr>
</cfloop>
<tr>
<td><cfinput type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</cfform>
For this to work, you need to have unique NAMEs for each input field. Right now, you're creating a bunch of inputs named either "C" or "D". If any one "C" is filled in, then no other field named "C" needs to be filled in. Create unique names using your index variable and the validation should work as you intend.
<cfloop from=1 to=5 index=i>
</tr>
<td><cfinput type="text" name="C_#i#" size="50" required="yes message="please enter your text"></td>
</tr>
</tr>
<td><cfinput type="text" name="D_#i#" size="50" required="yes message="please enter your text"></td>
</tr>
</cfloop>
There is a syntax error in the cfinput tag
It should be
<cfinput type="text" name="D" size="50" required="yes" message="please enter your text">
There is a missing quote after yes
In addition to what was said you should use something else and not cfform if you want anything more than basic validation. The cfform stuff is really legacy and there are much better options these days.

CFDocument Images

I am having a problem displaying images within cfdocument. I have tried several different options without luck (please see test code below).
It seems that cfdocument recognizes there is an image file there, because the size of the table cell changes. Yet still it doesn't display content of the image.
I would appreciate any help.
<cfdocument format="PDF" localURL = "yes">
<p style="background-color:#006633">
<table border="1">
<tr><td><cfoutput>
output from "file:///#ExpandPath('CEUCertificate_ABondy3.jpg')#"
</cfoutput>
</td>
<td style="background-color:white">
<cfoutput>
<image src="file:///#ExpandPath('CEUCertificate_ABondy3.jpg')#">
</cfoutput>
</td>
</tr>
<tr><td><cfoutput> output from #ExpandPath('CEUCertificate_ABondy3.jpg')# </cfoutput> </td>
<td>
<cfoutput>
<image src="#ExpandPath('CEUCertificate_ABondy3.jpg')#">
</cfoutput>
</td>
</tr>
<tr><td style="background-color:red">
Output from: img src="CEUCertificate_ABondy3.jpg"
</td>
<td style="background-color:red">
<img src="CEUCertificate_ABondy3.jpg"/>
</td>
</tr>
<tr>
<td style="background-color:white">
output from image src="CEUCertificate_ABondy3.jpg"
</td>
<td>
<cfoutput>
<image src="CEUCertificate_ABondy3.jpg">
</cfoutput>
</tr>
<tr>
<td>Output from local url</td>
<td>
<cfoutput>
<img src=#localUrl("CEUCertificate_ABondy3.jpg")#>
</cfoutput>
</td>
</tr>
</table>
</p>
</cfdocument>
<cffunction name="localUrl" >
<cfargument name="file" />
<cfset var fpath = ExpandPath(file)>
<cfset var f="">
<cfset f = createObject("java", "java.io.File")>
<cfset f.init(fpath)>
<cfreturn f.toUrl().toString()>
</cffunction>
For your initial question, you need to reference the images in your cfdocument tag as you would with any other HTML page; <img src="url-to-the-image" />. So this should work for you.
<img src="/cde/mobileweb/CEUCertificate_ABondy3.jpg"/>
Now that you have made that change the page is timing out. This is probably due to how the image was created. See this page here. Which I found from Charlie's page here.
Try opening that image in an editor and re-saving it. Then see if the processing time decreases.
I found some more discussion about this on Ben Nadel's blog here.
Instead of messing with ExpandPath() and file://, code it as a normal html and make sure it works first. Then use localUrl="true" in your <cfdocument>

ColdFusion 9 ReReplace nth occurence of HTML tag

I have the following HTML stored in a variable in ColdFusion 9. I need to insert a new table row after the 4th </tr>. i.e. before the Submit button.
<form name="form1" id="form1" action="" method="post">
<table>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref1" name="ref1" value="1" > </td></tr>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref2" name="ref2" value="2" > </td></tr>
<tr>
<th style="text-align:left;">Name * </th>
<td><input type="text" name="foo" id="foo" size="30" maxlength="50" value=""></td>
</tr>
<tr>
<th title="Please enter plain text or HTML." style="cursor:help;text-align:left;">Comment * </th>
<td><textarea name="bar" id="bar" cols="40" rows="10" ></textarea></td>
</tr>
<tr>
<th colspan="1"></th>
<td>
<input style="width:80px" type="submit" value="Submit">
<input style="width:80px" type="button" value="Cancel">
</td>
</tr>
</table>
ReReplace seems like the way to go, but I'm having trouble getting the regexp right. Another option would be to split the string and rebuild it with my new HTML in the middle. Any suggestions would be appreciated.
Regex is the wrong tool for this - you want a HTML parser.
Here's how you can do it with JSoup:
<cfsavecontent variable="InputHtml">
[insert code from question]
</cfsavecontent>
<cfsavecontent variable="NewRow">
<tr><th>whatever</th><td>stuff</td></tr>
</cfsavecontent>
<!--- Read "Creating Objects From Jar Files" --->
<cfset jsoup = createObject('java','org.jsoup.Jsoup') />
<cfset HtmlDom = jsoup.parse(InputHtml) />
<cfset HtmlDom.select('tr:eq(4)').after( NewRow ) />
<cfoutput>
<pre>#XmlFormat(HtmlDom.body().html())#</pre>
</cfoutput>
You can see details of what selectors are supported in the JSoup Selector API
If you don't know/care how many lines are in the table, you can do...
HtmlDom.select('table>tbody').append( NewRow )
...to just add the new row at the end.
Creating Objects From Jar Files
The above code most likely wont work instantly if you copy and paste it, because your server doesn't know about JSoup - you need to download the Jar file and put it in a sensible location.
For CF9, you need to copy the jsoup-1.6.3.jar into your {coldfusion}/lib directory then restart the server.
For CF10, you can use this.JavaSettings in your Application.cfc (as described here) to place it in a different location.
For Railo and OpenBD, you can specify the location of the JAR file as a third argument, e.g:
<cfset jsoup = createObject('java','org.jsoup.Jsoup','lib/jsoup-1.6.3.jar') />
I recommend doing this with jQuery:
​$(document).ready(function(){
$($('form tr')[3]).after('<tr><td>row</tr></tr>');
});​
Much easier.