kiat has asked for the wisdom of the Perl Monks concerning the following question:

Greetings to all,

I've a question regarding the reading of form submission. The html form in question is as follows:

<form name="myform" action="lgoin.pl" method="post"> <table <tr><th align="center" colspan="2">Login</th> <tr><td>Password</td><td><input type="text" name="pwd" size="5"> +</td></tr> <tr><td align="center" colspan="2"><input type="submit" name="lo +gin" value="doit"></td></tr> </table> </form> # portions of cgi code my $doit = param('login'); my %functions = ('login' => \&do_login, 'logout' => \&do_logout, ); if defined($doit) { &{ $functions{$doit} }; } else { default_page(); }
If the user clicks on the submit button 'doit', the submission is registered as desired. The value of the pwd field value is processed in the login sub.

However, if instead of clicking on the submit button, the user hits Enter after he has entered a value in the password field and while the cursor is still in the field, the form submission is not detected correctly.

How do you ensure that whether it's the button or Enter that is hit, the form submission is processed correctly?

I hope I've described what I'm trying to achieve in as clear a manner as possible.

I look forward to your enlightenment :)

Replies are listed 'Best First'.
Re: Form submit detection...
by edoc (Chaplain) on Dec 12, 2003 at 02:44 UTC
    <form name="myform" action="lgoin.pl" method="post"> <input type="hidden" name="login" value="doit"> ... <input type="submit" value="Login">

    you can now set the submit button value attribute as you wish..

    cheers,

    J

      Thanks, edoc!

      Does it make a difference if I've more than one field?

        nope, not at all. A "hidden" input is posted to the server just like any other input (text, select, etc) it just doesn't get displayed in the web page.

        cheers,

        J

Re: Form submit detection...
by etcshadow (Priest) on Dec 12, 2003 at 03:38 UTC
    If you just want to know whether or not the form was posted (as opposed to knowing whether or not the user clicked the "login" button... which is what you are presently testing), then you can just replace:
    my $doit = param('login');
    with:
    my $doit = $ENV{REQUEST_METHOD} eq 'POST';
    edoc's answer above, though, is also perfectly correct... and possibly a better answer. Still, checking whether the request is a GET or a POST is popular idiom.

    ------------
    :Wq
    Not an editor command: Wq