How to get a CGI program to start when someone clicks a button?

I have a Perl CGI program in which I created an HTML form. If someone clicks a button in this form, the CGI / Perl routine in this file is executed. Since I have several buttons on the form, I set their types to "Button", not "Submit."

This is a bookstore website, I have three buttons for books (for example, my buttons are “science fiction”, “fiction” and “poem”). and I have to use buttons. After pressing each button, a list of such books is displayed, and the user can select books. I should not use javascript: it should be controlled by CGI.

+3
source share
4 answers

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
}

+5
source

There are no restrictions on the number of buttons type="submit"that you can have in the form. As long as they have a name / value, only the name / value pair of the button used to submit the form will be sent to the server.

There are a few caveats due to browser support if you use the submit buttons or the item itself <button>, but in this case it doesn't look like it.

+2
source

, , . JavaScript...

, JavaScript!

?

+1

( ), : CGI, , "", , . , CGI.

, Perl , , . Perl CGI, CGI script, , . script GET POST , , script. Google PerlDoc CGI, .

+1

Source: https://habr.com/ru/post/1732674/


All Articles