I've already posted this to [EMAIL PROTECTED], but haven't had much
response, so I thought the cgi-list might be a more appropriate place
(apologies for cross-posting):
I want to read form data into a hash, using CGI.pm, and to take care of the
following cases:
- get rid of empty values
- decode multiple select fields *without* addressing them by name
- get rid of values "---" (for select fields where no selection is made)
- allow for two fields with the same name: A user selects an item from a
dropdown list or, if the desired item isn't there, adds it through an input
text field. If the text input field is chosen, the value for the select
field will be "---"; if the select field is chosen, there will be no value
for the input text field.
This is the code that I've tried using:
#!/usr/bin/perl
use CGI qw(:standard :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 a
similar deletion routine works with the following code (which, however,
does not decode multiple fields nor remove empty values):
sub parse_form {
my @names = $q->param;
foreach my $name ( @names ) {
if (param($name)) {
$in{$name} = $q->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? I was thinking that using the "Vars" method perhaps leaves me
with hash references in %in rather than actual values, and that that's why
it doesn't work.
I'd *really* be grateful for any advice - suggestions for alternative
solutions are, of course, also highly welcome.
Birgit Kellner
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]