• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Forms using cgi application

Hi all,

I am trying to get forms working with CGI::Application and HTML::Template, but with no success.  I have tried two methods which are listed below.

The first method seems to do nothing, while the second method loads up the starting index page.

Method 1:
=======

sub setup {
      my $self = shift;
      $self->start_mode('index');
      $self->mode_param('cmd');
      $self->run_modes(
            'index' => 'showIndexPage',
            'new' => 'showNewCustomerPage',
            'new2' => 'showNewCustomerPage2',
            'search' => 'showSearchCustomerPage',
      );
}

sub showNewCustomerPage {
      my $self = shift;
      
      my $q = $self->query();
      my $content = "";
      $content .= $q->start_form();
      $content .= $q->table({-border=>0,-cellpadding=>3,-cellspacing=>2,-align=>'center'},
            $q->Tr(
                  $q->td({-align=>'right'},   "First Name"),
                  $q->td(
                  $q->textfield(-name => 'first',-size => '40')),
            ),
            $q->Tr(
                  $q->td({-align=>'right'},  "Last Name"),
                  $q->td(
                  $q->textfield(-name => 'last',-size => '40')),
            ),
            $q->Tr(
                  $q->td({-align=>'right'},  " "),
                  $q->td(
                  $q->submit(-value => 'Add New Customer')),
            ));
      $content .= $q->hidden(-name => 'cmd', -value => 'new2');
      $content .= $q->end_form();
      return $content;

}

sub showNewCustomerPage2 {
      my $self = shift;

      my $q = $self->query();
      my $first=ucfirst(lc(param('first')));
      my $last=ucfirst(lc(param('last')));
      my $content = "";
        $content .= $q->p("Welcome $first $last");
      return $content;
}

Method 2:
=======

sub showNewCustomerPage {
      my $self = shift;
      
      my $q = $self->query();
      my $content = "";
      $content .= $q->start_form(-action=>'index.pl?cmd',-method=>'POST');
      $content .= $q->table({-border=>0,-cellpadding=>3,-cellspacing=>2,-align=>'center'},
            $q->Tr(
                  $q->td({-align=>'right'},   "First Name"),
                  $q->td(
                  $q->textfield(-name => 'first',-size => '40')),
            ),
            $q->Tr(
                  $q->td({-align=>'right'},  "Last Name"),
                  $q->td(
                  $q->textfield(-name => 'last',-size => '40')),
            ),
            $q->Tr(
                  $q->td({-align=>'right'},  " "),
                  $q->td(
                  $q->submit(-value => 'Add New Customer')),
            ));
      $content .= $q->end_form();
      return $content;

}

Any help will be most appreciated.

Thanks
Falthorn
0
falthorn
Asked:
falthorn
1 Solution
 
kanduraCommented:
Unless you want to show the start mode 'index', you will have to specify the runmode. In your case, you need to supply a value for cmd, either in your form, or in the url.

In the first method, you do this by creating a hidden field with name cmd  and value new2. However, you may be running into CGI's sticky behaviour here: if there was a parameter named cmd submitted to the script, then CGI won't put your 'new2' value in 'cmd', but it will use the submitted value instead. You can override this behaviour by using the -override attribute:

    $q->hidden(-name => 'cmd', -value => 'new2', -override => 1);

In your second method, you use the url for passing the name of run mode, but you didn't supply a value:

    $q->start_form(-action=>'index.pl?cmd',-method=>'POST');

This makes cgi-app call the start_mode runmode. If you specify a value here, it should work:

    $q->start_form(-action=>'index.pl?cmd=new2',-method=>'POST');

0
 
falthornAuthor Commented:
Hi Kandura,

Thanks again for the help.  I went for the second method in the end.  The first method just did the same as before even with the override added.

Thanks

Falthorn
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now