Gossamer Forum
Home : General : Perl Programming :

Form parsing with cgi.pm

Quote Reply
Form parsing with cgi.pm
Okay, this is probably a really simple question but heres what I'm doing. I'm writing a script that will parse a form using cgi.pm (first time using cgi.pm to parse a form, don't laugh). What I would like to do is filter out ceratin characters from all fields. The only way I can get this to work now is something like

$cgi->param('field1') =~ s/[\*]//g;
$cgi->param('field2') =~ s/[\*]//g;
....and so on.

Isn't there a way to do this with one line of code?
Quote Reply
Re: [BennyHill] Form parsing with cgi.pm In reply to
Code:
foreach $name ($cgi->param)
{
$value = $cgi->param($name);
$value =~ s/[\*]//g;
}

- wil
Quote Reply
Re: [BennyHill] Form parsing with cgi.pm In reply to
I;m actually writing a form processor inspired by the thread in the discussion forum and just wrote the code to do a similar thing which can be customized in the config file.

For example I have.....

BAD_CHAR => ['"', '`', ';', '#', '*'], # Disallowed characters

.......in the config file for webmasters to specify disallowed characters and then in the main code.......

Code:
foreach my $param ($in->param()) {

$i++;

$in{$param} = $in->param($param);
next unless ($param eq $required[$i - 1]);

!$in{$param} and push @errors, "Please fill in the $param field.<br>";

if ($cfg->{FILTER_CHAR}) {
foreach (@{$cfg->{BAD_CHAR}}) {
next if defined $found_c{$param};
if ($in{$param} =~ /\Q$_\E/) {
my $res = $in{$param};
$res =~ s,([$badc]),<font color="#FF3333"><b>$1</b></font>,g;
push @errors, "Disallowed character(s) in the $param field - $res.<br>";
$found_c{$param} = 1;
}
}
}
}

What that code does is spot bad chars in each field and then turn them red and push all the errors into @errors.