How do I cause a CGI program to run when someone clicks on a button?

button, cgi, perl

Solution

It should be as simple as, in your form:

<input type="submit" name="Button1" value="Button 1" />
<input type="submit" name="Button2" value="Button 2" />
<input type="submit" name="Button3" value="Button 3" />

And, in your script:

if ($cgi->param('Button1')) {
    # Button1 was clicked, do stuff...
} elsif ($cgi->param('Button2')) {
    # Button2 was clicked, do something else...
} elsif ($cgi->param('Button3')) {
    # Button3 was clicked, do whatever you want...
} else {
    # No button clicked; show the form
}

Problem

I have a Perl CGI program in which I designed an HTML form. If somebody clicks on a button in this form, a CGI/Perl subroutine in this file is executed. Because I have more than one buttons in the form, I set their types as "Button", not "Submit". This is a bookstore website, I have three buttons each for a kind of books (for example, my buttons are: "science fiction", "fiction" and "poem"). and I have to use buttons. After clicking each button, a list of books of that kind is presented and the user can select the books. I should not use javascript: it should be controlled by CGI.

Original source