Reputation: 41
I have an issue with a Perl CGI form which have two submit buttons. Let me explain with a sample code.
print $q->start_form(
-name => 'main',
-method => 'POST',
);
print $q->popup_menu( #drop down list
-name => 'popup1',
-values => @test,
-default => 'value2'
print $q->submit(
-name => 'do one thing',
);
print $q->submit(
-name => 'do two things',
);
if ($q->param("do one thing")) {
do certain functions;
}
if ($q->param("do two things")) {
##########output a checkbox#############
print $q->checkbox_group(
-name => 'checkemail',
-values => @test2,
-columns => 2,
-rows => 10,
);
###############submit the checkbox###########
print $q->submit(
-name => 'Submit',
);
if ($q->param("Submit")) {
do certain functions;
}
The code is simple. When we click "do one thing" button it should do certain function. When we click "do two things" its should display a checkbox. The form works fine until this. After the checkbox there is a submit button to submit this checkbox and do certain function. Unfortunately if I click submit button to submit checkbox it doesn't do the task after submit button. Also it even don't display the checkbox after we click the submit button of checkbox. Any help is appreciated.
Upvotes: 0
Views: 75
Reputation: 241748
Your indentation is chaotic. It might be a part of the problem: you misplaced the code that handles the Submit button. It is only run if the "do two things" button was pressed, but it is not possible to press both the buttons. Fixed (and runnable) code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $q = 'CGI'->new;
my @test = qw(a b);
my @test2 = qw(A B);
print $q->start_form( -name => 'main',
-method => 'POST',
);
print $q->popup_menu( #drop down list
-name => 'popup1',
-values => \@test,
-default => 'value2');
print $q->submit(-name => 'do one thing');
print $q->submit(-name => 'do two things');
if ($q->param('do one thing')) {
print "One thing: ", $q->param('popup1'), "\n";
}
if ($q->param('do two things')) {
print $q->checkbox_group( -name => 'checkemail',
-values => \@test2,
-columns => 2,
-rows => 10,
);
print $q->submit(-name => 'Submit');
}
if ($q->param('Submit')) {
print 'Sencond thing', $q->param('checkemail'), "\n";
}
Notice the code uses array references in -values
.
Upvotes: 2