tags:

views:

199

answers:

4

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.

+1  A: 

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?

Flo
this is a bookstore website, I have 3 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. I think your first solution is better for me. Could you please explain it a little more, or by an example. I really appreciate your any other guidance.Thanks again.
Shadi
Thanks for your reply and guidance :)
Shadi
+2  A: 

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.

Gareth
I changed the types of all my buttons to "submit" and then check their values to know which button is pressed.your guidance gave me a good insight about how to deal with my problem.Thank you so much.
Shadi
+1  A: 

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.

GreenMatt
Thank you.I used your guidance of "having a single CGI script" and then "realizing which button is pressed."It was very helpful.thanks.
Shadi
+4  A: 

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
}

David Precious
Thank you so much.your answer really helped me. However, because I am new in this site, it seems that I can not vote.Thanks
Shadi
Shadi, you can accept the answer by clicking the check mark (http://meta.stackoverflow.com/questions/5234/accepting-answers-whats-it-all-about/5235#5235)
Matthew Flaschen
@Shadi you should be able to upvote now. :-)
GreenMatt