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

I am writing a simple text editing routine for use in a web app. What I want to do in the following code is print the textfile to screen if the textfile exists otherwise, enter some text and save it off to the file - so when I hit submit, the data saves and displays. This works fine. If the text is empty, I don't want it to save - this is where Im having some issues - When I hit submit and have nothing entered in the bottom textarea, it still updates the top textarea ive tried things like
if (defined(param('SERVER_NOTES')) =~ m/\w+/ )
and
if (! defined(param('SERVER_NOTES)) !~ m/\W+/ )
amongst several others...

any hints would be appreciated
#!/usr/bin/perl -T use strict; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use Data::Dumper; #$CGI::POST_MAX=1024 * 100; # max 100K posts #$CGI::DISABLE_UPLOADS = 1; # no uploads # Clean up our UNIX environment # for more infor read perldoc perlsec $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; $|++; my @text; if ( defined (param('NOTES')) =~ m/\w+/ ) { @text=param('NOTES'); open (SERVER_NOTES, ">/var/www/cgi-bin/notes") or die("Unable to open notes: $!\n"); for my $text(@text) { print SERVER_NOTES "$text\n"; } close (SERVER_NOTES); } ## Start HTML ## print header; print start_html(-title=>"AWI Printer Administration - printer notes", -BGCOLOR=>"#cccc99" ), "\n"; print "<tt><center>\n"; print h3("AWI", "Printer", "Administration"),"\n"; print "</center>\n"; print hr,"\n"; # End header print br,br; if ( -s "/var/www/cgi-bin/notes" ) { print Delete_all(); open FH, "/var/www/cgi-bin/notes" or die("unable to open notes: $!\n"); my @new_text=<FH>; print "<center>\n"; print textarea('SAID',"@new_text", 15, 80, 0), "\n", br, br; print "</center>\n", br; #for (@text) { print }; } print br,br,br; print start_form; print "\n<center>\n"; print h3("Change printer notes"); print textarea('NOTES',"", 15, 80), "\n",br; print submit('Submit'); print "</center>\n",br; print Dumper @text; print end_html;

Thanks
Ted
--
"Men have become the tools of their tools."
  --Henry David Thoreau

Replies are listed 'Best First'.
Re: Simple CGI text editing
by Joost (Canon) on May 04, 2005 at 14:22 UTC
      Thanks, that did the trick

      Ted
      --
      "Men have become the tools of their tools."
        --Henry David Thoreau