I have tried the following code to read a query-string from a CGI request 
into a hash, taking into account (a) multiple select fields without knowing 
the field name, (b) getting rid of names without values, and thereby, 
hopefully, (c) allowing for user input alternately through a text field or 
a select list, (d) getting rid of select fields where no selection was 
made, in which case the value will be "---".

This is the code I used:

#!/usr/bin/perl
use CGI qw(:standard);
use CGI qw(:cgi-lib);
my $q = new CGI;
my %in = &parse_form;


sub parse_form {
my %in = $q->Vars;
foreach my $key (keys %in) {    
        unless ($in{$key}) { delete ($in{$key});}# get rid of empty values      
        if ($in{$key} eq "---") { delete ($in{$key});}# get rid of unselected 
selects 
        $in{$key} =~ s/\0/~~/g; #use "~~" as separator for multiple sepects
}
return (%in)
}

The problem is that the hash key/values which are empty are not deleted, 
nor are those pairs deleted where the value is "---". I'm puzzled because 
it works with the following code (which, however, does not decode multiple 
fields nor remove empty values):

sub parse_form {
my @names = $in->param;
foreach my $name ( @names ) {
        if (param($name)) {
        $in{$name} = $in->param($name);}
        }
foreach my $key (keys %in) {
        if ($in{$key} eq "---") { # if this is a select field with no value
                delete ($in{$key});
                }

return(%in);
}

Any ideas why the deletion doesn't work in the first "parse_form" 
subroutine?

Birgit Kellner


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to