doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

hi monks

I am trying to submit the CGI form with multiple buttons along with submit button. My intention is to select one of the preset and press SUBMIT button. This isn't working as preset button value is not passed or didn't get submit when I actually press the SUBMIT button. I am getting values for rest of the form fields (not included in the script). Can somebody help me? Thank you.

use strict; use CGI; my $q = new CGI(); print $q->header(); print $q->start_html(-title => 'Test HTML'); my $namescript = 'http://' . $ENV{'SERVER_NAME'} . (($ENV{'SERVER_PORT +'} != 80) ? ":$ENV{'SERVER_PORT'}" : "") . $ENV{'SCRIPT_NAME'}; print $q->startform("POST", $namescript); my %presets = ( 'preset_appleid' => 'APPLEID', 'preset_default' =>'DEFAULT', 'preset_ntlm' =>'NLTM', 'preset_security' => 'SECURITYQ', 'preset_web16' => 'Web Site (16 Chars)', 'preset_web32' => 'Web Site (32 Chars)', 'preset_wifi' => 'WIFI', 'preset_xkcd' => 'XKCD', ); foreach my $preset ( keys %presets ) { print $q->button( -name => $preset, -id => $preset, -value => $presets{$preset}, ); } print $q->submit(-value => 'SUBMIT'); print $q->endform; print $q->end_html();

Replies are listed 'Best First'.
Re: How to handle multiple buttons inside CGI form
by graff (Chancellor) on Feb 03, 2015 at 05:41 UTC
    If I understand you correctly, you either want to use radio buttons (if you want to allow only one choice from your presets to be selected) or checkboxes (if you want to allow any number of presets to be selected).

    You can refer to the parts of the CGI man page that describe "CREATING A RADIO BUTTON GROUP" or "CREATING A GROUP OF RELATED CHECKBOXES" (to be found under "CREATING FILL-OUT FORMS").

    In either case, all "preset" buttons are associated with a single parameter, and the value of that parameter will depend on which radio button (or which checkbox(es)) the user clicks before hitting the "submit" button.

      Thank you for your valuable suggestion. I implemented Radio Button group but one of the value is getting selected by default. Unless user select any choice, how can i deselect radio buttons without turning them on??

        Maybe provide an "opt out" button and make that the default so that that is an explicit option. After all, after the user has made a choice they can't go back to the original page state unless you provide an additional button.

        Perl is the programming world's equivalent of English
        Maybe this... http://perldoc.perl.org/CGI.html#PRAGMAS
        -nosticky By default the CGI module implements a state-preserving behavior called "sticky" fields. The way this works is that if you are regenerating a form, the methods that generate the form field values will interrogate param() to see if similarly-named parameters are present in the query string. If they find a like-named parameter, they will use it to set their default values. Sometimes this isn't what you want. The -nosticky pragma prevents this behavior. You can also selectively change the sticky behavior in each element that you generate.