John Pretti wrote:

-----Original Message-----
From: JupiterHost.Net [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 06, 2004 3:17 PM
To: John Pretti
Cc: [EMAIL PROTECTED]
Subject: Re: Form Validation




John Pretti wrote:


Hi all,


Hello,


What is the best way to validate a form element? I am using the following
code to generate a set of radio buttons; how can I ensure that one of the
buttons is checked before processing the rest of the script?



Assuming %label is

hw => 'H W here',
nw => 'North West',
etc...

you could simply do:

my $promdir = CGI::param('promdir');
die 'Invalid input' if !exists $label{$promdir};



print   $q->start_multipart_form,
               $q->p("Please select a directory to promote the file:"),
               $q->radio_group (
                       -name=>'promdir',
                       -values=>


I'd use -values => [keys %label],
for consistancy and sanity's sake


['hw','nw','sw','swmfis','swunifi','swwebcaaf'],
                       -labels=>\%label,
                       -default=>'selected',
                       -linebreak=>'true'),
               $q->br,
               $q->submit('Promote'),
               $q->end_form;

Thanks in advance,


HTH
Lee.M - JupiterHost.Net


John


OK now I am really lost. Here is what I am not following, the first time the
form is run it will have no parameters so the form will just be presented;
however if someone clicks promote without selecting a directory it will
still be blank, so how can perl determine when the promote button has been
pressed and blank parameters are being passed to the script. I have included

Incude a hidden field, say like name="action" value="runme"

my $promdir = CGI::param('promdir');
if( CGI::param('action') ) {
  die 'Invalid input' if !exists $label{$promdir};
} else {
  # do form
}

or check if its defined

my $promdir = CGI::param('promdir');
if( defined $promdir ) {
  die 'Invalid input' if !exists $label{$promdir};
} else {
  # do form
}

my code below. The script in its current state works perfectly, I just want
to add an extra level of checking. Thanks in advance. -John

#!/usr/bin/perl -w

# perlPromote.cgi by John Pretti
# Comments/Questions john[at]web-connected.com
# Last modified 05/17/04

# Load needed Perl modules
use strict;
use diagnostics;
use CGI; #Make HTML east to deal with
use CGI::Carp 'fatalsToBrowser'; # Report errors to Browser
use File::Slurp;

# SCP commands
my $scp_cmd = "/usr/bin/scp";
my $scp_opts = '-i /home/apache/.ssh/id_dsa ';

# Current Working Directory
my $src_dir = "/www/web/htdocs/merlin/upload";

# Promotion directories
my @rem_hosts = ('x.x.x.x', 'y.y.y.y', 'z.z.z.z');
my $working_dir = "home/rdwebadmin/support_docs";
my %directories_map =
("hw","$working_dir/HW","nw","$working_dir/NW","sw","$working_dir/SW","swmfi
s","$working_dir/SW/MFIS","swunifi
","$working_dir/SW/Unifi","swwebcaaf","$working_dir/SW/WEBCAAF_eforms");

# Create New CGI Object
my $q = new CGI;

my %label = ('hw'=>'..support_docs/HW',
             'nw'=>'..support_docs/NW',
             'sw'=>'..support_docs/SW',
             'swmfis'=>'..support_docs/SW/MFIS',
             'swunifi'=>'..support_docs/SW/Unifi',
             'swwebcaaf'=>'..support_docs/SW/WEBCAAF_eforms');

        if ( $q->param() ) {


ok add it here:

             croak 'Invalid input' if !exists $label{$q->param('promdir')};

        # Promote the file to merlin
        foreach my $var(@rem_hosts) {
        system(`. /home/apache/apache_beigep1_dsa.dat;$scp_cmd $scp_opts
$src_dir/* [EMAIL PROTECTED]:/$directories_map{$q->param("pr
omdir")}`);
        }
        system(`rm -f $src_dir/*`);

# Print success print $q->header();
print read_file('../../htdocs/merlin/htmlheadps.html');
print $q->p("The file has been promoted to the MERLIN Servers.");
print read_file('../../htdocs/merlin/htmlfootps.html');


        # Print form on first run
        } else {
        print   $q->header();
        print   read_file('../../htdocs/merlin/htmlheadp.html');
        print   $q->start_multipart_form,
                $q->p("Please select a MERLIN directory to promote the
file:"),
                $q->radio_group (
                        -name=>'promdir',
                        -values=>
['hw','nw','sw','swmfis','swunifi','swwebcaaf'],
                        -labels=>\%label,
                        -default=>'selected',
                        -linebreak=>'true'),
                $q->br,
                $q->submit('Promote'),
                $q->end_form;
        print   read_file('../../htdocs/merlin/htmlfootp.html');
        }





-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to