Does anyone know how to set up a large number of textfields for data
input and then parse them conveniently?  In the CGI.pm book it shows
how to use the form element 'textfield' like so:

#!/usr/bin/perl -wT
use CGI::Carp qw(fatalsToBrowser);
use CGI ':standard';
use CGI::Pretty;
use strict;
use diagnostics;
my $x = new CGI;
print $x->header;
print $x->start_html;
 print $x->center($x->br, $x->h1("Here are three textfields:\n"),
$x->startform(-method=>"POST",-action=>"parsnip.cgi"),
textfield(-name=>'X1',-size=>5.),
textfield(-name=>'X2',-size=>5.),
textfield(-name=>'X3',-size=>5.),
$x->p,
$x->submit( -name=>"Submit"));
print $x->endform;
print $x->end_html;


and I would like to check that the fields contain no special
characters:

#!/usr/bin/perl -wT
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard -no_xhtml);
#use CGI ':standard';
use CGI::Pretty;
use strict;
use diagnostics;
my $x = new CGI;

my $X1=param('X1'); my $X2=param('X2'); my $X3=param('X3');
if(
      ($X1 !~ /^[\d.]+$/) && ($X1 !~ /^\s*$/)
   || ($X2 !~ /^[\d.]+$/) && ($X2 !~ /^\s*$/)
   || ($X3 !~ /^[\d.]+$/) && ($X3 !~ /^\s*$/) )
{
        print $x->header;
        print $x->start_html(-title=>'parsnip',-bgcolor=>'silver');
        print $x->font({color=>"blue"});
        print $x->center( "<br>", $x->h1("Error in input!\n"),
        $x->h1(" Use digits and decimal points only!\n"),
        $x->h2(" Exiting!\n") );
        exit;
}else{
        print $x->redirect(-URL=>'done.cgi');
}

with the 'done.cgi' script:

#!/usr/bin/perl -wT
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard -no_xhtml);
#use CGI ':standard';
use CGI::Pretty;
use strict;
use diagnostics;
my $x = new CGI;

        print $x->header;
        print $x->start_html(-title=>'done',-bgcolor=>'silver');
        print $x->font({color=>"blue"});
        print $x->center( "<br>", $x->h1("Done!\n");
        exit;

The problem is that if I want to collect data from lots of textfields,
like maybe 100, I get huge, sloppy scripts where I have to do a lot of
typing for the variables X1-X100 and I make a lot of mistakes.  I bet
there's a better way in the Perl world to do this.  I tried making an
array going from 1 to 100 and calling the variables "X.$_" but it
didn't work.
Is there a better way that anyone knows about?  Thank you.




-- 
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/


Reply via email to