Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding newlines Textarea data

Status
Not open for further replies.

oaklandar

Technical User
Feb 12, 2004
246
0
0
US
Please advise how I can handle Textarea that is coming from form entries where if sometone types in an entry and hits enter to enter new data it would show up on different lines on my action page.

For example in my Textbox with a field called city:
The user would enter several cities in the form textarea like this:

San Francisco
San Diego
Denver


My form would then process this and show on my action page as:

San Francisco San Diego Denver


I want it to show with newlines on my action page just as it was entered in the textbox:

San Francisco
San Diego
Denver



Here is my attempt:
Code:
#!/usr/local/bin/perl -w


#use strict, warnings, web cgi, and browser error modules
use strict;
use CGI qw(:standard); 
use CGI::Carp qw(fatalsToBrowser);

my $fetch_data = '';
my ($field, $value);

foreach $field (param)
{
  #added this loop to cover multivalued form fields to
  #process each value individually if needed 
  foreach $value (param($field))  
  {
     if($field !~ /city/)  #process other fields normally
     {
         $fetch_data .= "<tr><td>$field:</td><td>$value</td></tr>\n";
     }
     elsif($field =~ /city/) #process textarea field
     {
         $textareaField .= "City\:\t$value\n\n<br\>";
     }
  }


}
print header, <<"EOF";
<HTML>
<BODY>
<table>
$fetch_data
$textareaField
</table>
</HTML>
EOF
 
The textarea field actually returns not individual linefeeds, but carriage return/linefeed pairs:

San Francisco\r\n
San Diego\r\n
Denver\r\n

You were close - but you need either a loop or better yet a regex to fix EACH line within the city param. Try this instead:
Code:
     elsif($field =~ /city/) #process textarea field
     {
         $textareaField = $value;
         $textareaField =~ s/[\r\n]+/<br>\n/g;
     }
Printed back to the browser, this will look like:

San Francisco
San Diego
Denver

In your sample code, however, you seemed to be trying to print each city on a line prefaced vby "City:\t". If you'd rather do that, this will work:
Code:
     elsif($field =~ /city/) #process textarea field
     {
         $textareaField = $value;
         $textareaField =~ s/([^\r\n]+)[\r\n]*/City:\t\1<br>\n/g;
     }
In both cases, I have allowed for the fact that there may or may not be a trailing CR/LF pair at the end on its own line.

Printed back to the browser, this will look like:

City: San Francisco
City: San Diego
City: Denver


To further modify the output format, you can just adjust the formatting in the replace part of the regex:
Code:
City:\t\1<br>\n
In this example, the \1 contains the city name. Everything else is straightforward. (I hope.)

--G

 
Hi oaklander,

should be a very simple fix to your problem ...

you could try adding text performatting tags, <pre> </pre>, around the submitted textarea value ... these understand carriage returns, tabs etc... and are displayed appropriately on HTML output

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top