Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Processing Textarea data

by Anonymous Monk
on May 17, 2004 at 14:53 UTC ( #353984=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a textarea box where Individual names are entered sepearted by Carriage Return. I want the results (on the action page and the email) to come back with names seperated by Carriage Return just as they were entered in the textbox. The emails come back with names seperated by CR but the results page puts the names all on one line with no Carriage return sepeartion. Please advise how I can correct this?
foreach my $field (param) { foreach my $value (param($field)) { $name .= "Names here\:\t$value<br />"; } }
Mail part and results page:
open (MAIL,"|$sendmail"); print MAIL <<"EOF"; To: '' From: '' Subject: 'Names' $name EOF close MAIL; print header, <<"EOF"; <HTML> <BODY> $name </BODY> </HTML> EOF

Replies are listed 'Best First'.
Re: Processing Textarea data
by EvdB (Deacon) on May 17, 2004 at 15:05 UTC
    Try making this change:
    from: foreach my $value (param($field)) to: foreach my $value ( split /\n/, param($field) )
    You need to take the scalar value that you get from param and turn it into an array.

    --tidiness is the memory loss of environmental mnemonics

Re: Processing Textarea data
by atcroft (Abbot) on May 17, 2004 at 15:10 UTC

    My first guess is that your email is plain text-correct? In HTML, carriage returns in the source are not displayed (unless within <pre></pre> tags). So, I see two possibilities (at least):

    • in the section where you display the results, place a set of <pre> tags around $name, or
    • after sending the email, but before displaying the results, do a search and replace on \n with <br />\n.

    For my part, I would try the first one (as it would be the easier of the two).

    Hope that helps.

      Tricky question, eh? I think this is a wrong answer, see, the poster already adds <BR /> so that the newlines show up in the html. I almost got fooled with this one too.

      Update: Sorry, atcroft, this post was a bit too harsh (or cheeky). I'm now not so sure if param really returns an array or a scalar here. I'd still guess scalar, because anonymonk says in the question

      The emails come back with names seperated by CR
      Note also that in ruby, for on a string iterates through its lines (as defined by $/, the record separator); which may be the source of confusion.

      Anyway, adding a <PRE> is a good solution in either case.

        Actually, given that the email displays them as the OP desires, but that he wants the results to show up on the web page in the same manner, <pre></pre> tags around the variable will in fact take care of the issue, without having to worry about looking at the parameter in question as a scalar or array.

        Test results, result display section, CGI based on OP's provided code
        TestHTML codeDisplayed
        Sample results, original code
        <html> <body> Names here: duh<br />Names here: joe bob mary<br /> </body> </html>
        Names here: duh
        Names here: joe bob mary
        Sample results, <pre></pre> tags around $name
        <html> <body> <pre> Names here: duh<br />Names here: joe bob mary<br /> </pre> </body> </html>
        Names here:	duh
        Names here: joe bob mary

        HTML code, for reference:

        <html> <head> </head> <body> <form action="/cgi-bin/" method=post> <table> <tr><td>Department</td> <td><input type=text name=department></td> </tr> <tr> <td colspan=2>People</td> </tr> <tr> <td colspan=2> <textarea name=people rows=5 cols=40></textarea> </td> </tr> <tr> <td><input type=submit></td> <td><input type=reset></td> </tr> </table> </form> </body> </html>

        perl code, for reference:

        #!/usr/bin/perl use CGI qw/:standard/; $| = 1; foreach my $field (param) { foreach my $value (param($field)) { $name .= "Names here\:\t$value<br />"; } } # Mailer section removed from OP's code for testing print header, <<EOF; <HTML> <BODY> <pre> $name </pre> </BODY> </HTML> EOF

        Hope that helps.

        Actualy he only puts the br after the value. It looks like its printing "Names:" followed by all the names grouped together. Viewing the source would probably show all the names on seperate lines. I think atcroft is quite right here but then we need the OP to be a little more specific about what he _is_ getting.

        Eric Hodges
Re: Processing Textarea data
by particle (Vicar) on May 17, 2004 at 15:04 UTC

    you need to put in a newline, if you want it to appear...

    $name .= "Names here\:\t$value<br />\n"; ## ^^

    ~Particle *accelerates*

Re: Processing Textarea data
by EdwardG (Vicar) on May 17, 2004 at 15:13 UTC

    One of the advantages of the Web is that text is automatically wrapped into lines fitting within the current window size. Sometimes though, you will want to disable this behavior. For example when including samples of program code.



Re: Processing Textarea data
by ambrus (Abbot) on May 17, 2004 at 15:15 UTC

      foreach my $value (param($field))

    This loop won't iterate on the lines of the cgi parameter. param($field) will return only one value, so the loop body is ran only once.

      of course param($field) returns all values belonging to $field, so the loop will iterate over them. i think you're forgetting multivalued parameters...
        Yes - in the case of multivalued parameters param will return an array, but in this example the OP is spcifically reffering to a textarea field which results in a scalar.

        --tidiness is the memory loss of environmental mnemonics

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://353984]
Approved by particle
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2022-05-18 19:39 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.