" tag [ By: Jim Summer ]"/>

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

About This Tutorial
Author: Jim Summer
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5
Total Views: 136,512
Submission Date: December 13, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 5
Discuss This Tutorial
  • @ Chin Kwan How To flip it back when editing the form, convert the
    into Chr(13). Ex:

    #replace(queryname.comments,"
    ",Chr(13), "all")#

    Then when you display the contents of the form in the html, convert the Chr(13) back into
    for display purposes.

    #replace(form.comments,Chr(13),"
    ", "all")#

    And yes as it was pointed out by Kevin, it would be better to use

    tags and it is only slightly more coding. You would start the display with a

    , then replace 2 consecutive Chr(13) with

    , then close the whole thing with a closing

    . That way, if the client hits the enter key twice, they are actually creating a true

    break instead of multiple
    tags. Which more often than not would be the intent and would also be semantically correct html. --Jim

  • How I convert back the
    to enter? When I editing the paragraph in the form? Because I don't want the
    show in the form.

  • thanks for providing this page... you guys really help. thanks a lot.

  • If it truly is a new paragraph, make it so!
    is a line break. this will make a new paragraph so you can format it correctly with your css.. you are using css....right??

    #replace(form.comments,Chr(13),"

    ", "all")#

    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.

  • I do the same thing but with a #chr(10)#. What is the difference?

  • 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.

  • ghg d df fdgdf

  • Thank you oh mighty ColdFusion God!!! I hate having to ask my clients to format their articles with

    tags.... very lame indeed!!!! THANK YOU!!

Advertisement


Website Designed and Developed by Pablo Varando.