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

I have a form that is filled out and after it is submitted I display all the field and values. But I want the web page to display each field with its values on newlines:
City: San Diego
State: California
Country: USA


Currently the page displays this after the form is submitted:
City: San Diego State: California Country: USA

Here is the perl script. Please advise.
#!/usr/local/bin/perl -w #use strict, warnings, web cgi, and browser error modules use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $fetch_data = ''; my ($field, $value); foreach $field (param) { #added this loop to cover multivalued form fields to #process each value individually if needed foreach $value (param($field)) { $fetch_data .= "$field\:\t$value\n\n"; } } print header, <<"EOF"; <HTML> <BODY> $fetch_data </HTML> EOF

Replies are listed 'Best First'.
Re: Displaying values after form is submitted
by sschneid (Deacon) on May 12, 2004 at 13:13 UTC
    Modify this:
    $fetch_data .= "$field\:\t$value\n\n";
    ...to be this:
    $fetch_data .= "$field\:\t$value<br />";
    Remember: you're printing it as HTML, so you'll need HTML line breaks.

    -s.
Re: Displaying values after form is submitted
by matija (Priest) on May 12, 2004 at 13:31 UTC
    You can't format HTML with tabs. Well, you kinda can, if you wrapt that part in <pre> </pre> tags. But that looks extremely ugly.

    You should find and read a decent HTML tutorial.

Re: Displaying values after form is submitted
by Thelonius (Priest) on May 12, 2004 at 13:52 UTC
    Consider using a table:
    #!/usr/local/bin/perl -w #use strict, warnings, web cgi, and browser error modules use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $fetch_data = ''; my ($field, $value); foreach $field (param) { #added this loop to cover multivalued form fields to #process each value individually if needed foreach $value (param($field)) { $fetch_data .= "<tr><td>$field:</td><td>$value</td></tr>\n"; } } print header, <<"EOF"; <HTML> <BODY> <table> $fetch_data </table> </HTML> EOF
      Thanks to all it works great. The only area I now need newlines for a specific field is where the user enters information in a textarea box on my form:
      Enter the city or cities

      and the person could enter several cities in the textbox area where they type in the city and hit the enter key and then type in another city such as this in the textbox:
      San Francisco
      San Diego


      Currently that entry will show up as this on my web page results:
      City: San Francisco San Diego

      Anyway to have just the textarea entries show up like this with a newline after each city entered?
      City: San Francisco
      San Diego

      Here is my attempt:
      foreach $value (param($field)) { if($field !~ /city/) { $fetch_data .= "<tr><td>$field:</td><td>$value</td></tr>\n"; } elsif($field =~ /city/) { $textareaField .= "City\:\t$value\n\n<br\>"; } } } print header, <<"EOF"; <HTML> <BODY> <table> $fetch_data $textareaField </table> ---- <TEXTAREA name="city" ROWS="4"></TEXTAREA>
Re: Displaying values after form is submitted
by eXile (Priest) on May 12, 2004 at 15:35 UTC

    If not only the poster but also other people are to see the filled-in-values-page then I'd suggest you use tainting (discussed in the perlsec manpage, just type perldoc perlsec). Suppose somebody filled in the value:

    <img src="www.site.com/realcheesyadvertisement.gif">
    your page will display an advertisement. This is relatively harmless, but worse things can be done by adding some javascript code to the filled-in value (this is called "cross site scripting").

    A solution is tainting-mode (perl -T), where you can only use variables 'from the outside' if you've untainted them first. This untainting is done by checking all variables against a regex, that for instance only allowes \w-characters.