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

Hi, I have a form with three submit buttons on it. Can someone tell me how to tell which button was pressed within my script? Thanks in advance, Tom.

Replies are listed 'Best First'.
Re: form buttons
by stefan k (Curate) on Nov 30, 2001 at 15:09 UTC
    Usually you give your submit button a value and a name:
    <input type="submit" name="action" value="Search"> <input type="submit" name="action" value="Logout">
    and if you
    use CGI;
    you can then test for what action to do:
    if (param("action") eq "Search") { # ... } # ...

    Please take into account that this comes from the back of my head and I have not been doing CGIs for more than a year now; still it should be correct.

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

Re: form buttons
by Anonymous Monk on Nov 30, 2001 at 15:12 UTC
    Give all the buttons the same name but different values.
    <input type="submit" name="action" value="button1"> <input type="submit" name="action" value="button2"> <input type="submit" name="action" value="button3">
    Then in your CGI the action parameter will be 'button1' or 'button2' or 'button3' depending on which is pressed.
    my $query = CGI::new(); #assign value to $action my $action = $query->param('action');
    you can then say
    if ($action eq 'button1') { #do your thing }elsif ($action eq 'button2'){ #do another thing }
Re: form buttons
by kwoff (Friar) on Nov 30, 2001 at 20:09 UTC
    Also, sometimes you want to separate the buttons in different <FORM> sections.