Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to preserve line breaks in TEXTAREA...question please

    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;



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

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Posts
    368
    Thanks
    10
    Thanked 4 Times in 4 Posts
    They are removing newlines with this regex:

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

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,874
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Question duplicated across multiple forums.

    http://forums.devshed.com/perl-progr...se-605213.html

    http://perlguru.com/gforum.cgi?post=...=unread#unread

    I'm sure that there several others.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •