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.

Graphic Designer / Web Designer / Graphic Artist
Next Step Systems
US-IL-Des Plaines

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

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

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES