I have a simple Survey like form in which I am using Perl CGI to do so, no interactive HTML docs will be used. The issue I'm having is that I don't know how to determine which submit button was clicked and if so either proceed to the next question or go to the previous question. I have set values to the buttons but I'm not sure on how to use that value to proceed to the next question. (I am fairly new at Perl CGI, please pardon the sloppy code. Thanks)
#!c:/Perl/bin/perl.exe
use CGI;
use CGI::Carp qw(fatalsToBrowser);
if ( $ENV{'REQUEST_METHOD'} eq "POST" ) {
$form_size = $ENV{'CONTENT_LENGTH'};
read( STDIN, $form_data, $form_size );
} else {
$form_data = $ENV{'QUERY_STRING'};
}
$form_data =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
@fields = ( split( /&/, $form_data ) );
$size = @fields;
for ( $i = 0; $i < $size; $i++ ) {
( $key, $value ) = ( split( /=/, $fields[$i] ) );
$value =~ s/[;<>\(\)\{\}\*\|'`\&\$!#:"\\]/\ /g; # Syntax Highlighting fix: '
$value =~ s/[+]/\ /g;
$my_hash{$key} = $value;
}
my $q = new CGI;
print $q->header("text/html"), $q->start_html( -title => "Survey", -bgcolor => "blue" );
@labels = ( "Not at all", "", "Somewhat", "", "Extremely" );
@labels2 = ( "No", "Yes", "N/A" );
print $q->submit( 'prev_button', 'Prev' );
print $q->submit( 'next_button', 'Next', 'Javascript: validate_form()' );
if ( submit . value == Prev ) {
if ( $size == 0 ) {
$size = 0;
} else {
$size = $size - 1;
}
} elsif ( submit . value == Next ) {
if ( $size == 6 ) {
$size = 6;
} else {
$size + 1;
}
}
if ( $size == 0 ) {
print $q->p("1. Please enter your name:");
print $q->textfield( 'q1', '', 50, 80 );
} elsif ( $size == 1 ) {
print $q->p("2. Did you find the information included within this web site useful?");
print $q->radio_group( 'q2', [ '1', '2', '3', '4', '5' ], "-", 'false', \%labels );
} elsif ( $size == 2 ) {
print $q->p("3. Is the web site easy to navigate?");
print $q->radio_group( 'q3', [ '1', '2', '3', '4', '5' ], "-", 'false', \%labels );
} elsif ( $size == 3 ) {
print $q->p("4. Were you able to find the information you were looking for?");
print $q->radio_group( 'q4', [ '1', '2', '0' ], "-", 'false', \%labels2 );
} elsif ( $size == 4 ) {
print $q->p("5. What other information would you like to be included in the web site?");
print $q->textarea( 'q5', '', 10, 50 );
} elsif ( $size == 5 ) {
print $q->p("6. What suggestions you might have for our website improvement?");
print $q->textarea( 'q5', '', 10, 50 );
} elsif ( $size == 6 ) {
print $q->p("Thank you for your input.");
}
print $q->end_html;
exit(0);
Use the param
method:
if ($q->param('prev_button') eq 'Prev') {
...
if ($q->param('next_button') eq 'Next') {
It's better to use the same name for all the submit buttons, as it's not possible to press more than one at the same time. Then, you can use a dispatch table based on the value.
my $other_page = { Prev => \&previous_page,
Next => \&next_page,
}->{ $q->param('submit') };
$other_page->() if $other_page;