开发者

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

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 "Su开发者_如何学编程bmit".

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.


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
}


There's no limit to the number of type="submit" buttons you can have in a form. So long as they have a name/value set, only the name/value pair of the button used to submit the form will be sent to the server.

There are a couple of caveats due to browser support if you're using image submit buttons, or the <button> element itself, but it doesn't sound like that's happening in this case.


You could assign a different name to distinguish them on the server side, if it is neccassary. Otherwise you could try to submit your form with JavaScript...

I'm sure you can implement it with JavaScript!

What do you want to do exactly?


Once I had a similar (I think) but easier situation: I had a CGI program generate a form, and when the "Submit" button was clicked I wanted the same form to be run. For this, it was simply a matter of having the form's action attribute call the same CGI program.

For your situation, it seems you have multiple Perl scripts to call, depending on which button is pressed. I've not used Perl for CGI much, but one way to do this would be to have a single CGI script which is called regardless of which button is called. That script then inspects the GET or POST values and figures out which button was pressed, then calls the appropriate subroutine or script. Google found the PerlDoc CGI page, which should help you get started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜