Gossamer Forum
Home : Products : DBMan : Customization :

Multiple Select Not Passing All Selected Values to Database

Quote Reply
Multiple Select Not Passing All Selected Values to Database
I am using a multi-select mod (and have used it many times in the past very successfully). For my new project, I am having an issue where my multiple selections are not being logged into the database. Only the first item I select in the list is being captured.

Here is my code:

sub build_sizes {
# --------------------------------------------------------
#
# To call this subroutine from html_record_form, use the following syntax:
#
# print &build_sizes("Sizes",$rec{'Sizes'});
#
# Be sure to express the field name *exactly* as it is defined in your .cfg file.
#
$size = "8";
my $field = $_[0];
my $compare = $_[1];
my %selector = (
'Sizes' => [
['1.0','1.0'],
['1.5','1.5'],
['2.0','2.0'],
['2.5','2.5'],
['3.0','3.0'],
['3.5','3.5'],
['4.0','4.0'],
['5.0','5.0'],
]
);
$output = qq|<SELECT NAME="$field" MULTIPLE SIZE="$size">\n|;
$i = 0;
while ( $selector{$field}[$i][0] ) {
$compare =~/$selector{$field}[$i][0]/ ?
($output .= qq|<OPTION VALUE="$selector{$field}[$i][0]" SELECTED>$selector{$field}[$i][1]\n|) :
($output .= qq|<OPTION VALUE="$selector{$field}[$i][0]">$selector{$field}[$i][1]\n|);
++$i;
}
if ($i) {
$output .= "</SELECT>";
}
else {
$output = "Incorrect field definition";
}
return $output;
}

In past use, the database would capture somethng like this: 1.0~~2.0~~2.5

Again, now it is only capturing my first selection. Is there something I need to add or modify in any of the subs in the db.cgi file or am I missing something else. Please help. My brain is fried trying to figure out what is wrong.

Thanks!
Quote Reply
Re: [BrianYork] Multiple Select Not Passing All Selected Values to Database In reply to
Brian:

I'm not a programmer but I looked at another example of other coding for multiple select fields and suggest you try changing:

($output .= qq|<OPTION VALUE="$selector{$field}[$i][0]" SELECTED>$selector{$field}[$i][1]\n|) :
($output .= qq|<OPTION VALUE="$selector{$field}[$i][0]">$selector{$field}[$i][1]\n|);

to:

($output .= qq|<OPTION VALUE="$selector{$field}[$i][0]" SELECTED>$selector{$field}[$i][1]\n!) :
($output .= qq|<OPTION VALUE="$selector{$field}[$i][0]">$selector{$field}[$i][1]\n!);

that's changing:

\n|) to: \n!);

Hope that works, and if not, someone with more knowledge will come along and help you out.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Multiple Select Not Passing All Selected Values to Database In reply to
Thanks for the reply. Tried that and it cause the script to cgi error.

I still can't figure it out.
Quote Reply
Re: [BrianYork] Multiple Select Not Passing All Selected Values to Database In reply to
I found the problem by working backwards, BUT CANNOT FIGURE OUT HOW TO FIX IT.

I started with a fresh install of basic DBman, then I added the file upload mod by JPDeni. Once I changed sub parse_form in the db.cgi as required by the file upload mod, that is where the problem comes into play.

Here is the original sub parse_form:

sub parse_form {
# --------------------------------------------------------
# Parses the form input and returns a hash with all the name
# value pairs. Removes SSI and any field with "---" as a value
# (as this denotes an empty SELECT field.
my (@pairs, %in);
my ($buffer, $pair, $name, $value);
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr ("This script must be called from the Web\nusing either GET or POST requests\n\n");
}
PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g; # Remove SSI.
if ($value eq "---") { next PAIR; } # This is used as a default choice for select lists and is ignored.
if ($value eq "http://") { next PAIR; } # Removes default beginning of URLs
unless ($value) { next PAIR; } # Eliminates false multiple selections
(exists $in{$name}) ?
($in{$name} .= "~~$value") : # If we have multiple select, then we tack on
($in{$name} = $value); # using the ~~ as a seperator.
}
return %in;
}

Here is the revised sub parse_form as in the mod:

sub parse_form {
# --------------------------------------------------------
my (%in);
my ($buffer, $pair, $name, $value);
PAIR: foreach $name ($query->param()) {
$value = $query->param("$name");
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
if ($value eq "---") { next PAIR; }
unless ($value) { next PAIR; }
(exists $in{$name}) ?
($in{$name} .= "~~$value") :
($in{$name} = $value);
}
return %in;
}

Anyone know what to revise in order to allow multiple selections?

Thanks!
Quote Reply
Re: [BrianYork] Multiple Select Not Passing All Selected Values to Database In reply to
Figured out the fix.

If you are using the file upload mod in conjunction with a multi-select form, you need to replace sub parse_form in db.cgi with this code:

sub parse_form {
# --------------------------------------------------------
my (%in);
my ($buffer, $pair, $name, $value);
PAIR: foreach $name ($query->param()) {
@value = $query->param("$name");
$value = join '~~', @value;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
if ($value eq "---") { next PAIR; }
unless ($value) { next PAIR; }
$in{$name} = $value;
}
return %in;
}

You will now be able to capture multiple selections and upload a file as well (both of which are independent functions).
Quote Reply
Re: [BrianYork] Multiple Select Not Passing All Selected Values to Database In reply to
Glad you got it figured out Smile

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/