Yes, you can use a multiple select field for inputting data. Where you run into problems is having a multiple select field in a search form.
This is what I did. I set up one form for searching (html_search_form) and one for adding and modifying (html_record_form). Within the html_record_form definition, I used
Code:
<TR><TD align=right valign=top><$font>Category:</font></td>
<TD>|;
print &build_mult_select_field("Category",$rec{'Category'},3);
print qq|<BR><$font>Hold the <B>Ctrl</B> key to select more than one.</font>
<BR><input type=text name="Category" size="20" maxlength="255">
<$font>Add a new category</font></td></tr>
You'll notice there is a select field and a text field with the same name. That's so I can add a new category on the fly without having to change my .cfg file. I can even add more than one category, if I put a | between them in the text field.
Okay. Now for the subroutine. I made a new one so I wouldn't lose my old build_select_field_from_db subroutine.
The following goes into db.cgi, around where all the "build" subroutines are.
Code:
sub build_mult_select_field {
# --------------------------------------------------------
# Builds a SELECT field based on information found
# in the database. Parameters are the column to build,
# a default value, a default name,
# and the size of the box.
my ($column, $value, $size, $name) = @_;
my (@fields, @values, $output);
$name | | ($name = $column);
$size | | ($size = 1);
@values = split (/\Q$db_delim\E/,$value);
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name.
Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
my (@tempfield) = split (/\Q$db_delim\E/o, $fields[$fieldnum]);
foreach $field (@tempfield){
if (!(grep $_ eq $field, @selectfields)) {
push (@selectfields, $field);
}
}
}
close DB;
$output = qq|<SELECT NAME="$name" MULTIPLE SIZE=$size><OPTION>---|;
foreach $field (sort @selectfields) {
($value =~ $field) ?
($output .= "<OPTION SELECTED>$field") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";
return $output;
}
When you use the subroutine, use
print &build_mult_select_field("FieldName",$rec{'FieldName'},
3);
The
3 is the number of rows your select field will show.
There's one other thing you need to do.
In db.cgi, sub parse_form
after
if ($value eq "---") { next PAIR; }
add
unless ($value) { next PAIR; }
As I said, though, you will run into trouble if you use this in a search form. I'd use build_select_field_from_db on the form that searches.
You will need to change that subroutine, too, though, to break up your multiple selections.
Code:
sub build_select_field_from_db {
# --------------------------------------------------------
# Builds a SELECT field from the database.
my ($column, $value, $name) = @_;
my (@fields, $field, @selectfields, @lines, $line, $ouptut);
my ($fieldnum, $found, $i) = 0;
$name | | ($name = $column);
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name.
Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
@options = split (/\Q$db_delim\E/o, $fields[$fieldnum]);
foreach $option (@options) {
if (!(grep $_ eq $option, @selectfields)) {
push (@selectfields, $option);
}
}
}
close DB;
$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @selectfields) {
($field eq $value) ?
($output .= "<OPTION SELECTED>$field") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";
return $output;
}
You probably can put the two subroutines together. But this works for me.
IMPORTANT!! Don't forget to take out the spaces between the | | characters!!!!! ------------------
JPD
[This message has been edited by JPDeni (edited May 13, 1999).]