CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
PRESERVING Line break in perl

 



serazoo
New User

Apr 19, 2009, 4:57 PM

Post #1 of 3 (2212 views)
PRESERVING Line break in perl Can't Post

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;




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


1arryb
User

Apr 20, 2009, 4:18 PM

Post #2 of 3 (2194 views)
Re: [serazoo] PRESERVING Line break in perl [In reply to] Can't Post

Hi Sandra,

1. Please indent your code in future posts. People won't thank you if they (like I) have to spend time prettying it up before studiying it. (hint: use 'code' tags).

2. I highly recommend that you "use strict;" and "use warnings;" at the top of your programs. I do and got many errors and warnings when compiling your program. Makes it a little hard to see real problems.

(So, enough with the meta, you say?) Ok...

3. I'm assuming that data is "saved into the database" in save_new_items()? If so, I see that you explicitly remove line termination at:

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

Could this be it? Or am I missing something?

Cheers,

Larry


FishMonger
Veteran / Moderator

Apr 20, 2009, 5:09 PM

Post #3 of 3 (2191 views)
Re: [1arryb] PRESERVING Line break in perl [In reply to] Can't Post

Larry,

serazoo has cross posted this in several other forums.

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives