0

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);
Miller
  • 34,962
  • 4
  • 39
  • 60
user3600460
  • 63
  • 1
  • 3
  • 10
  • `submit . value` is not a Perl syntax. – choroba Nov 17 '15 at 21:22
  • 3
    You should add `use strict; use warnings;` to the top of your script and to every Perl script you ever write; they can help catch a lot of simple errors. See [Why use strict and warnings?](http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings) for details. – ThisSuitIsBlackNot Nov 17 '15 at 21:30
  • Wow Thanks a lot. I am fairly new to the language and the requirements make it even more difficult because I can't use Javascript and again like you said, it is a dead language. I do have a couple of further questions regarding this code. Although I have now set limitations on my $size variable, how do I get the form to generate the next question in my nested If statement at the cluck of the submit button? It seems like it should be relatively simple but I am getting stuck. – user3600460 Nov 18 '15 at 21:10

2 Answers2

1

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;
choroba
  • 231,213
  • 25
  • 204
  • 289
0

You seem to be rather guessing at how a lot of this stuff works. As you're using the CGI module, it would probably be a good idea to read its documentation once or twice before spending any more time trying to use it.

Some points about your code.

  1. Always have use strict and use warnings in your code. And fix all of the problems they will show you.
  2. The CGI module has a param() method which gives you access to the parameters passed to your program. Using that will enable you to lose all that horrible parameter parsing code at the top of your program.
  3. Please don't use the new CGI syntax to create a new CGI object. CGI->new is far less error prone.
  4. The HTML creation methods in the CGI module are deprecated. Please consider using a templating system instead. You could also consider a PSGI-based solution rather than CGI (which is considered a dead technology) See CGI::Alternatives for suggestions.

Your actual problems seem to be caused by some syntax errors in your code.

if ( submit . value == Prev ) {

This is not valid Perl. I would hope that it would lead to syntax error messages being written to your error log. As you have two submit buttons, you probably want:

if ($my_hash{prev_button}) {
    ...
} elsif ($my_hash{next_button}) {
    ...
}

But, as others have said, it's probably better for both submit buttons to have the same name.

Things are a little strange inside your if ... else ... block too. This:

if ( $size == 0 ) {
    $size = 0;
} else {
    $size = $size - 1;
}

Can be simplified to:

$size-- if $size != 0;

And this:

if ( $size == 6 ) {
    $size = 6;
} else {
    $size + 1;
}

Doesn't do anything useful. I think you wanted:

if ( $size == 6 ) {
    $size = 6;
} else {
    $size = $size + 1;
}

Which can be simplified to:

$size++ if $size != 6;
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Wow Thanks a lot. I am fairly new to the language and the requirements make it even more difficult because I can't use Javascript and again like you said, it is a dead language. I do have a couple of further questions regarding this code. – user3600460 Nov 18 '15 at 21:05
  • Woah. I didn't say that Perl was a dead language. I said that CGI was a dead technology. There are plenty of good ways to write web apps using Perl. – Dave Cross Nov 18 '15 at 21:17