Server Time:
Tuesday May 13 2008 10:04 AM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Replacing "enter" key with "<br>" tag
by: Jim Summer
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

This is a little problem I ran into when I first got started in CFML. I see this question asked a lot so I figured I would address it. I searched high and low and never got the definitive CFML answer, so I tweaked how it is done in ASP a little and it worked!

  • First file #1) is the form
  • 2nd file #2) is the form handler that replaces "enter" keys with "<br>"

==========================================
1) The first part is the form. It submits to the code in # 2) below.
----------------------------------------
<html>
<head>
  <title>
Replace BR in TextArea</title>
</head>
<body>

  <form method="post" action="textarea_ac.cfm">
    <textarea name="comments" cols="50" rows="10"></textarea>
    <br>
    <input type="submit" value="Submit">
  </form>

</body>
</html>



==========================================
2) Here is the form handler, it just displays it on the page for simplicity, but it could easily be going into a database...
----------------------------------------
<html>
<head>
   <title>
Replace BR in TextArea</title>
</head>
<body>

   <cfif isdefined("form.comments") AND (#form.comments# NEQ "")>
      <cfoutput>
        
<p>#replace(form.comments,Chr(13),"<br>", "all")#</p>
      </cfoutput>
   <cfelse>
      <p>You silly... go back and enter some text!</p>
   </cfif>
   <p><a href="javascript:history.go(-1);"><< Back</a></p>
</body>
</html>

==========================================
Thank you,
Jim Summer


Date added: Fri. December 13, 2002
Posted by: Jim Summer | Views: 16884 | Tested Platforms: CF5 | Difficulty: Beginner
Categories Listed: Best Practices Functions

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Search Engine Bot Notifier
This code detects the most common user agents (web browsers) and notifies you via email if it is not a recognized user agent as defined in the code. Usually this will be a bot of some sort. Extrememly useful for tracking how often Google, Yahoo, etc visits your site. It will email you the bot and a reverse IP lookup url with the IP appended so you can verify if it is a "good bot" or a "bad (spam) bot" (then you can block that IP or stop processing of the page). Use this in your home page or as an include file throughout your site. Nothing fancy it should work on MX also although I have not tested it there. - Date added: Wed. April 14, 2004
Navigation as an include file
Create an include file (custom tag) for your navigation to help make maintenance easier! This include file lights up the button depending on where the user is at in your website, and is XHTML 1.1 validated! If you need to edit your navigation, just do it in 1 place, the include file! Javascript included :) - Date added: Thu. December 12, 2002
Aliasing Your SQL Statements
Many developers I have talked to are not aware of the ability to create an "alias" in an SQL statement, concatenate, and even add strings into your SQL statements. A little work up front in the SQL can cut out a lot of tedious coding in the <cfoutput> tag. - Date added: Tue. December 10, 2002
Database Dates (between ranges)
This deals with database dates: (1)inserting a properly formatted date into the database, and then (2)pulling a query from the database between a date range defined by 2 text boxes. - Date added: Mon. December 9, 2002
Recordset Paging in Cold Fusion
This will pull a predefined number of records from a database, allow the user to change the number of records to be shown, and write the "NEXT" or "BACK" (or both) buttons at the bottom of the page. Thus allowing the user to "surf" through the database. See it in action at http://freecfm.com/t/tentonhead/ and click on the "CTCS" link when you get there. The HTML for this page has changed a little since I first did this (so it validates as XHTML1.1) but the CFML remains as it is in this snippet. - Date added: Mon. December 9, 2002
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
I bow at your feet!!
Thank you oh mighty ColdFusion God!!! I hate having to ask my clients to format their articles with <p> tags.... very lame indeed!!!! THANK YOU!!
Posted by: Brian Snider
Posted on: 07/24/2004 05:15 PM
f
ghg
d
df


fdgdf
Posted by: ff
Posted on: 09/03/2004 06:55 AM

that works for the intial adding of an article but if you edit the article later, it will double up your br tags. atleast with the udf i made back in the day that would happen.
Posted by: matt
Posted on: 10/09/2004 04:34 PM
What about chr(10)?
I do the same thing but with a #chr(10)#. What is the difference?
Posted by: ash
Posted on: 07/08/2005 10:37 PM
another way....
If it truly is a new paragraph, make it so! <br/> is a line break. this will make a new paragraph so you can format it correctly with your css.. you are using css....right??

<p>#replace(form.comments,Chr(13),"</p><p>", "all")#</p>

as for chr(10) vs chr(13)

one is a carriage return, one is a new line. two different characters depending on the OS mostly.
Posted by: Kevin
Posted on: 11/24/2006 07:59 PM
very good
thanks for providing this page...
you guys really help.
thanks a lot.

Posted by: nwel
Posted on: 08/30/2007 02:34 AM
Convert back
to ENTER

How I convert back the <br> to enter?
When I editing the paragraph in the form?
Because I don't want the <br> show in the form.
Posted by: Chin Kwan How
Posted on: 01/24/2008 11:23 PM
Just reverse it when editing the form
@ Chin Kwan How
To flip it back when editing the form, convert the <br> into Chr(13).
Ex:
<p>#replace(queryname.comments,"<br>",Chr(13), "all")#</p>

Then when you display the contents of the form in the html, convert the Chr(13) back into <br> for display purposes.
<p>#replace(form.comments,Chr(13),"<br>", "all")#</p>

And yes as it was pointed out by Kevin, it would be better to use <p> tags and it is only slightly more coding. You would start the display with a <p>, then replace 2 consecutive Chr(13) with </p><p>, then close the whole thing with a closing </p>. That way, if the client hits the enter key twice, they are actually creating a true <p> break instead of multiple <br> tags. Which more often than not would be the intent and would also be semantically correct html.

--Jim
Posted by: Jim Summer
Posted on: 02/08/2008 02:46 PM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
ColdFusion Hosting by HostMySite

You are 1 of 684 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb