...

View Full Version : How to preserve line breaks in TEXTAREA...question please



serazoo
04-20-2009, 01:11 AM
Hello all...

please help, I've tried so many combination of scripts...I'm going bananas :)

I've would like my textarea to preserve all line breaks when saved into the database (.txt) file

please tell what could be the magic line to insert and where ?

cheers

Sandra :)



here's my script:

-------------------------------------------

sub new_items{

$form{'function'} eq "save_new_items" and &save_new_items;


# Section Heading
###########################
print qq~
<P>
<table border="0" width=98%" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="800000"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="ffffff"><b>Add New Items</b></font></td>
</tr>

<tr>
<td bgcolor="000000"><img src="space.gif" width="1" height="2" vspace="0" hspace="0"></td>
</tr>

<tr>
<td>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
<ul>
&nbsp;<br>
$save_message
~;

$counter = 1;

foreach (@table_fields)
{
$size = "";
$fieldsize{$counter} and $size = qq~size="$fieldsize{$counter}"~;

!$form{$counter} and $form{'function'} ne "preview" and $form{$counter} = $default_input{$counter};

$datatype{$counter} eq "Text" and $input = qq~<input type=text name="$counter" value="$form{$counter}" $size>~;
$datatype{$counter} eq "Textarea" and $input = qq~<textarea name="$counter" cols="46" rows="20"

$fieldsize{$counter}>$form{$counter}</textarea>~;
$datatype{$counter} eq "Option" and do{

@options = split (/\n/, $optionfields{$counter});
$input = qq~<select name="$counter">\n~;

foreach $option(@options)
{
$selected = "";
$form{$counter} eq $option and $selected = "selected";

$input .= qq~<option $selected>$option</option>\n~;

}#end foreach option

$input .= qq~</select>~;

};#end option do


$datatype{$counter} eq "Checkbox" and do{

$checked = "";
$form{$counter} and $checked = "checked";
$input = qq~<input type=checkbox name="$counter" value="1"> $optionfields{$counter}~;

};#end checkbox do

print qq~
<font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="">
<b>$_:</b></font><br>
$input<P>
~;
$counter++;
}
print qq~
</ul>
</td>
</tr>
</table>
<p>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="">
<center>
<b>
<input type=hidden name="menu" value="new_items">
<input type=radio name="function" value="save_new_items" checked> Save Directly
<input type=radio name="function" value="preview"> Preview<P>
</b>
<input type=submit value="Send New Entry">
<P>
</center>
~;


}

###########################################
sub save_new_items
{
$error_message = "";
$new_line = "";
$counter = 1;
&load_db;


foreach (@table_fields)
{
$requiredfield{$counter} and !$form{$counter} and $error_message .= qq~<b>$_</b> is a required field, and nothing has been entered.<br>~;
$uniquefield{$counter} and !$form{$counter} and $error_message .= qq~<b>$_</b> is a unique, required field, and nothing has been entered.<br>~;

# Check Required Status
#######################################
$uniquefield{$counter} and $form{$counter} and do{

foreach $keys (keys %data_hash)
{$data_hash{$keys}{$_} eq $form{$counter} and $error_message .= qq~<b>$_</b> is required to be a unique field, and has been found in the

database at line $keys.<br>~;}

};#end required field do


# Remove New Lines
######################################
$form{$counter} =~ (s/\r\n/ /g);

# Create New Line By Adding Next Field
######################################
$new_line .= qq~$form{$counter}|~;

$counter++;
}

# Save if No Errors
######################################
!$error_message and do{
open (FILE, ">>$data_file");
print FILE qq~$new_line\n~;
close (FILE);
chmod(0666,"$data_file");

%form = ();

$save_message = qq~<b>New Record Has been Saved</b><P>~;
};# end save to file

# Report Errors if Present, Return
######################################
$error_message and do{

$save_message = qq~New Record Has <b>Not</b> been Saved. Please correct the following:<P>$error_message<P>~;

};# end save to file

}#end save new items


1;



------------------------------------------

netroact
04-20-2009, 11:33 PM
They are removing newlines with this regex:



# Remove New Lines
######################################
$form{$counter} =~ (s/\r\n/ /g);

bazz
04-21-2009, 01:42 AM
Hi Sandra,

Maybe I misunderstand but, preserving the new lines in your db may not be a good idea. It's your call of course but, something to think about might be that when you preserve the breaks into the db, they will be there when you retrieve them for display in a web page. if the user of your pages has a larger screen than you, won't they then find the line breaks stop short of the area in the page? A bit like a text paragraph 200px wide in a page section 500px wide.

If you remove them for display in the page, then you will have made two processes on the data, perhaps, needlessly.

If your plan to store line breaks is to make it easier for you to read the flat file, then it may be easier for you just to use word wrap in yout fav text editor.

Just thought I'd chip in, in case you hadn't thought all the way through yet.


hth

bazz

FishMonger
04-21-2009, 02:14 AM
Question duplicated across multiple forums.

http://forums.devshed.com/perl-programming-6/how-to-preserve-line-breaks-in-textarea-question-please-605213.html

http://perlguru.com/gforum.cgi?post=37334;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unrea d

I'm sure that there several others.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum