[lug] perl and cgi.pm
Tkil
tkil at scrye.com
Thu Feb 7 10:44:43 MST 2002
>>>>> "Michael" == Michael J Hammel <mjhammel at graphics-muse.org> writes:
Michael> I'm trying to add a checkbox group to a CGI.pm script. I
Michael> need to give the values for each checkbox inside a loop.
I don't understand this part of the question.
A checkbox group is a set of HTML checkboxes, all with the same name,
but different values, and [typically] diffrent checked/unchecked
states.
The easiest way to do it is to specify all these at once:
my @cb = $q->checkbox_group( -name => 'my_group',
-values => [ qw( foo bar baz quux ) ],
-default => [ qw( foo baz ) ] ) );
Each element of the array is a simple string containing the HTML
representing each checkbox; you can manipulate this anyway you like.
One thing I've done with it is to split a list into two columns:
my $left_cb = join '<br />', @cb[ 0 .. $#cb/2 ];
my $right_cb = join '<br />', @cb[ $#cb/2+1 .. $#cb ];
print $q->Tr( $q->td( { -valign => 'top' }, [ $left_cb, $right_cb ] ) );
Michael> According to Lincoln Stein's book on CGI.pm, the function
Michael> checkbox_group() will return an array of elements (in an
Michael> array context) that I can manipulate in my loop (which
Michael> actually builds a table).
Michael> My question is I don't know what elements I'm getting back or
Michael> how to manipulate them.
I think I answered this above -- they're strings. As for how to find
that out, may I suggest debugging prints?
| $ perl -MCGI=:standard -lwe \
| 'print join "\n", checkbox_group( -name => "my_group",
| -values => [ qw( foo bar baz quux ) ],
| -default => [ qw( foo baz ) ] );' \
| < /dev/null
| > > > > (offline mode: enter name=value pairs on standard input)
|
| <INPUT TYPE="checkbox" NAME="my_group" VALUE="foo" CHECKED>foo
| <INPUT TYPE="checkbox" NAME="my_group" VALUE="bar">bar
| <INPUT TYPE="checkbox" NAME="my_group" VALUE="baz" CHECKED>baz
| <INPUT TYPE="checkbox" NAME="my_group" VALUE="quux">quux
Michael> So how do I access the elements created by the call to
Michael> checkbox_group and update/set the value for a given checkbox?
You have to be careful with the terminology here. Do you really want
to change the *value* of the checkbox within a group, or just modify
its checked / unchecked state? Either way, you could muck about with
the strings using regex replacements -- but that feels fragile to me.
Better to just create them with the correct values in the first place.
I typically create all the boxes up front, then pick them off as I
need them. Or, if it is reasonable to do so, I can also choose to use
a more list-processing / functional programming style, modifying the
list as I go. This works particularly well with CGI's Tr and td
methods, which do some magic mapping when appropriate:
my $table = $q->table( $q->Tr( [ map $q->td($_),
$q->checkbox_group( ... ) ] ) );
Using the same arguments to checkbox_group as above, this code yields
something like this:
| $ perl -MCGI=:standard -lwe \
| '@cb = checkbox_group( -name => "my_group",
| -values => [ qw( foo bar baz quux ) ],
| -default => [ qw( foo baz ) ] );
| $_ = table( Tr( [ map td($_), @cb ] ) );
| s+</tr>+$&\n+ig;
| print;' < /dev/null
| > > > > > >
| <TABLE>
| <TR><TD><INPUT TYPE="checkbox" NAME="my_group" VALUE="foo" CHECKED>foo</TD></TR>
| <TR><TD><INPUT TYPE="checkbox" NAME="my_group" VALUE="bar">bar</TD></TR>
| <TR><TD><INPUT TYPE="checkbox" NAME="my_group" VALUE="baz" CHECKED>baz</TD></TR>
| <TR><TD><INPUT TYPE="checkbox" NAME="my_group" VALUE="quux">quux</TD></TR>
| </TABLE>
Does this answer your questions?
t.
More information about the LUG
mailing list