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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove hard return from form textarea data

Status
Not open for further replies.

megp

Programmer
Aug 25, 2004
31
0
0
US
I have a website with a guest book entry form handled by a CGI program that saves the form data to a .txt database ($in{'name'}|$in{'email'}|$in{'comments'}\n), then redirects the user to a .cgi page that displays the guest book.

The problem is when there are hard returns in the comments section (a textarea on the form): Information contained following a hard return is viewed as a new entry. How can I either remove or escape the hard returns contained in the form data?

This is the code that the viewer is redirected to, displaying the guestbook at
#!/usr/bin/perl
require "cgi-lib.pl";
print &PrintHeader;

#Open database
open(FILE,"guestbook.txt") || die "Can't find database\n";

#Store contents of database in an array
@indata = <FILE>;

#Close database
close(FILE);

#Create page with submitted reservations
print <<"PrintTag";
<html>
<head>
<title>The Wedding of David Paladino and Amber Clevenger: Guest Book</title>

<link type="text/css" href="../davidandamber/wedding.css" rel="stylesheet">

</head>

<body>

<center>
<table class="gb" cellspacing="0" cellpadding="0">

<tr>
<td class="menu" height="500px"><a href=" class="m">Welcome</a><br>
<a href=" class="m">Events</a><br>
<a href=" class="m">Accommodations</a><br>
<a href=" class="m">Registry</a><br>
Guest Book<br>
&nbsp;&nbsp;<a href=" class="m">Sign the Guest Book</a><br>
<a href=" class="m">Contact Us</a></td>

<td class="text">
PrintTag

#Use a foreach loop to process each record in the database
foreach $i (@indata)
{
#Remove hard return from each record
chomp($i);
#Split fields on pipe character
#Assign a variable name to each of the fields
($name,$email,$comments) = split(/\|/,$i);
#Add a new row to the table for each record
print "$comments<br>\n";
print "&nbsp;&nbsp;&nbsp;&nbsp;-- <a class=\"mini\" href=\"mailto:$email\">$name</a><br>\n";
print "<hr>\n";
#Close the loop
}
#Close the table
print "</td></tr></table>";
print "</center></body></html>";

#End of script
 
maybe this will work for your script:

Code:
($name,$email,$comments) = split(/\|/,$i);
#Add a new row to the table for each record
$comments =~ s/\n\r|\n/ /g;
print "$comments<br>\n";

that should replace newlines or newlines\carraige returns with a space.

To keep formatting you could replace them with a <br> tag:

$comments =~ s/\n\r|\n/<br>/g;

 
KevinADC
You're great! Thank you. I couldn't get it to work quite the way you posted, but you got me on the right path. I took your code and used it in the original form handler that saves the form data to the database. Take out the hard returns there and replace them with "--" just to save the intended distinction, and all is well with the world.

This is what I needed in my code before it saved it in the database:

Code:
$in{'comments'} =~ s/\n/--/g;

Thanks again!
Meghan
 
Yes, you would have to remove the newlines from the form data before it gets printed to your file. The code you posted previoulsy didn't show how you were getting the form data to your script so I just used $comments as a generic variable to show how it might be done and you correctly adapted it to your script. Good job!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top