When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Sunday, November 21, 1999

Correctly Displaying the Results from a TEXTAREA


If you need to let your visitors enter a large amount of textual information, using TEXTAREAs are the way to go. TEXTAREAs are those large, multi-row text boxes. For example, here is a TEXTAREA with 40 columns and 5 rows, created using the following HTML code:

- continued -

<FORM> <TEXTAREA COLS=40 ROWS=5>Hello, world!</TEXTAREA> </FORM>

(For more information on TEXTAREA, see Microsoft's HTML Documentation.)

The user can enter information on several lines, and spaces and carraige returns can be used to format the text within the TEXTAREA. Imagine that we saved the results of the TEXTAREA into a database field, and, at some later time, wish to display that database field on a web page. When we attempt to display the results, they will all be on one line, and the spaces will be ignored! For example, say that someone enters into a TEXTAREA the following text:

My little horse must think it queer
  to stop without a farmhouse near
Between the wood and frozen lake,
  the coldest evening of the year.

If we attempt to display this (either directly when the form is submitted, or sometime later, if the results are stored in a database), the output on a web page will be:

My little horse must think it queer to stop without a farmhouse near Between the wood and frozen lake, the coldest evening of the year.

This is because when your browser renders HTML, it ignores whitespace. To signify a line break, you need to use the <BR> tag; to force a space, you need to use &nbsp;. A line break, when entered into a TEXTAREA is saved as a carraige-return-line-feed, vbCrLf. We can use the Replace function to replace all vbCrLfs with <BR> before we display the contents of the TEXTAREA. Similarly, we can replace all spaces with &nbsp;.

For example, if we create a TEXTAREA in a form, using the following code:

<FORM ACTION="DisplayTextAreaValue.asp" METHOD=POST> <TEXTAREA NAME=Poetry COLS=40 ROWS=5></TEXTAREA> <P> <INPUT TYPE=SUBMIT> </FORM>

the form processing script DisplayTextArea.asp will be loaded when the user clicks submit. We can then display the value entered by the user in the TEXTAREA with the following line of code in DisplayTextArea.asp:

Response.Write Request.Form("Poetry")

However, this will not display the carraige returns and spaces entered by the user in the TEXTAREA. To show the spaces and carraige returns, we will need to use the Replace function. We will begin by reading the value of Request.Form("Poetry") into a string variable- we will then use the Replace functiont twice, once to replace vbCrLfs with <BR> and once to replace spaces with &nbsp;. Here is the code!

'Read in the value of the TEXTAREA Dim strPoetry strPoetry = Request.Form("Poetry") 'Replace all vbCrLf with <BR>s strPoetry = Replace(strPoetry, vbCrLf, "<BR>") 'Replace all spaces with &nbsp; strPoetry = Replace(strPoetry, " ", "&nbsp;") 'Output the formatted TEXTAREA value: Response.Write strPoetry

That's it! If you are storing the result of the TEXTAREA into a database, you can perform these steps before you insert the record, or each time you need to display the results on an HTML page. I'd recommend using the latter approach, since using the former approach mixes the data for your application with the presentation of your application, which is ill-advised.

Happy Programming!


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES