memnoch has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks
I'm writing a CGI script in which each radio button may have sub-options (i.e. textfields, checkboxes), each of which is defined in a hash:
my %options = ( show_reqs => checkbox(-name=>'show_reqids', -checke +d=>0, -value=>'ON', -label=>'Show requirement IDs?'), date => "<b>Created: </b>" . radio_group(-name=>'bef_sin +ce', -values=>['before', 'since'], -default=>'none', -labels=>{'before' => 'before ', 'since' => 'since ( +inclusive)'}) . " <b>Date</b> (YYYY-MM-DD): " . textfield(-name=>'date', -size=>10, -maxlength=>10), req_type => "<b>Requirement Type</b> (e.g. BRQ): " . textfiel +d(-name=>'reqtype', -size=>5, -maxlength=>5), );
The names and options used for a particular radio-button are defined in another hash under the "opts" field:
Later on in the script, I loop over the keys of %report_types to generate the sub-options appropriate for each report type.my %report_types = ( 0 => {name => "Report One", opts => ['show_reqs', 'date'], coderef => \&report_typ +e_two}, 1 => {name => "Report Two", opts => ['show_reqs', 'req_type', 'date'], coderef => +\&report_type_three}, );
The problem is that when several textfields have the same name, they can step on each other when I try to access them. I was thinking that if I could assign a unique value to the -name field for each repeated sub-option, then I could avoid this. For example, report choices will be based on the keys of %report_types ("0" and "1"), and each will have a get a sub-option of "date" that contains a "date" textfield. The names I would like to give to these textfields are "date_0" and "date_1".
Is there any way to do this at runtime? I've been thinking that a codref might work, but I can't figure out how. Any ideas? Or is there a better approach altogether?
Update: I have put a standalone testcase in the "readmore" section below:
#!c:/perl/bin/perl.exe -T use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; use warnings; my $TITLE = "Reporting Utility"; # Options available for each report type my %options = ( show_reqs => checkbox(-name=>'show_reqids', -che +cked=>0, -value=>'ON', -label=>'Show requirement IDs?'), date => "<b>Created: </b>" . radio_group(-name=>'bef_si +nce', -values=>['before', 'since'], -default=>'none', -labels=>{'before' => 'before ', 'since' => 'sin +ce (inclusive)'}) . " <b>Date</b> (YYYY-MM-DD): " . textfield(-name=>'date', -size=>10, -maxlength=>10 +), req_type => "<b>Requirement Type</b> (e.g. BRQ): " . textfi +eld(-name=>'reqtype', -size=>5, -maxlength=>5), ); my %report_types = ( 0 => {name => "Project Requirements: Filtered +by Creation Date", opts => ['show_reqs', 'date'], coderef => \&report +_type_two}, 1 => {name => "Project Requirement Revisions: Filtered by +Creation Date, Type, and Version Reason", opts => ['show_reqs', 'req_ +type', 'date'], coderef => \&report_type_three}, ); # Hash for use with populating below radio_group my %radio_hash; foreach my $type (keys %report_types) { $radio_hash{$type} = $report_types{$type}{'name'}; } # Used to create the radio group of report types my @rep_type_rad_gr = radio_group(-name=>'report_type', -values=>[sort + {$a <=> $b} keys %radio_hash], -default=>'none', -labels=>\%radio_ha +sh); print header, start_html(-title=>$TITLE, -bgcolor=>"#ffcc99"), h2($TIT +LE), hr; my $query = CGI->new(); if (defined $query->param('report_type')) { # If the user has made a s +election my $show_reqids = "false"; if ($query->param('show_reqids') eq "ON") { $show_reqids = "true"; } print("<blockquote>\n"); print h3("$report_types{$query->param('report_type')}->{'name'}") +, "\n"; # Run report based on user selection, including all $query->param + values as arguments $report_types{$query->param('report_type')}{'coderef'}->($show_re +qids, $query->param('bef_since'), $query->param('date'), $query->para +m('reqtype'), $query->param('ver_reason')); $query->delete_all; print start_form; print defaults("Start over"); print end_form; print("</blockquote>\n"); } else { # User has not made a selection print("<blockquote>\n"); print p("<h3>Choose the type of report:</h3>\n"); print start_form; foreach my $i (0..$#rep_type_rad_gr) { # For each radio button print "<p><b>", $rep_type_rad_gr[$i], "</b>"; foreach my $option ( @{$report_types{$i}->{opts}} ) { # Print +the options associated with a given report type print "<br> ", $options +{$option}, "\n"; } print "</p>\n"; } print p(submit("Submit")); print end_form; print("</blockquote>\n"); } print end_html;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is there a way to set a textfield -name value at runtime?
by eric256 (Parson) on Jan 25, 2008 at 21:55 UTC | |
by memnoch (Scribe) on Jan 25, 2008 at 22:33 UTC | |
by eric256 (Parson) on Jan 26, 2008 at 04:47 UTC |