0
votes

I have a couple Perl CGI pages where there are multiple submit buttons. This is necessary as I need to use the form data to make changes to a DB (For example modify, delete, activate).

I don't know what layer is responsible but it seems that whichever submit button is displayed first on the form that is the one that will execute when you click "Enter" on the keyboard.

Here is a sample of my Submit buttons for one page:

...
      #
      # Modify button
      #
      $cgi->submit(
                   -name  => 'Action',
                   -value => 'Modify',
      ),

      #
      # Delete button
      #
      $cgi->submit(
                   -name  => 'Action',
                   -value => 'Delete',
      ),

      #
      # Activate button
      #
      $cgi->submit(
                   -name  => 'Action',
                   -value => 'Activate',
      ),
      #
      # Reset button
      #
      $cgi->submit(
                   -name  => 'Action',
                   -value => 'Reset',
      ),

      ),

      $cgi->end_form;

      $sth->finish;
    }

For this page I want the 'Modify' button to be the default (there are more submit buttons on the page that are inline with rows).

The closest thing I've found out there recommended using javascript submit buttons for all the buttons I don't want to be the default but, they said that I still needed to list the one I wanted as default at the top. That would defeat my purpose of placing the buttons any where and choosing one to be the default.

I also saw some options for possibly catching when the user clicks enter when using javascript.

Question: How can I set a default submit button when the user clicks the "Enter" key on a Perl CGI form, with multiple submit buttons, in any order?

Thank you for taking the time to look at this.

2
Thank you for providing this link and making me aware of this. In this link it provides an alternatives page: metacpan.org/pod/CGI::Alternatives. The page lists some other perl modules like Mojolicious and Dancer2. Do those modules work in conjunction with CGI.pm or are the replacements to it? - WhoYaGonnaCall
Those are alternatives to it. If you want to continue using CGI.pm then stick to the "RAW GCI.pm Examples" section. - Quentin

2 Answers

1
votes

I don't know what layer is responsible

That's basic handling of HTML forms in the browser.

but it seems that whichever submit button is displayed first on the form that is the one that will execute when you click "Enter" on the keyboard.

Correct

How can I set a default submit button when the user clicks the "Enter" key on a Perl CGI form, with multiple submit buttons, in any order?

At the beginning of the form, duplicate the submit button. Since it is now the first button, it will be triggered when the form is submitted by pressing Enter in a text input.

Set tabindex="-1" on it so it will be out of the normal focus order.

Position it off-screen so that it will not appear to users.

0
votes

There is an attribute in HTML called "hidden". It can be used to hide elements. Often times a condition must be satisfied before an element is shown.

In CGI.pm you can work-around the default submit button for the 'Enter' key by hiding a duplicate of the one you want. Add the button you want to be the default to the top of your form and add the '-hidden' attribute.

 $cgi->submit(-name => 'submit', -value => 'Modify', -hidden),