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

Hello Monks,

I have a webpage created using cgi which along with many other HTML elements, has one Submit button (button1) and another button of type button (button2). This button2 I want to use to run a DB query when it is clicked.

Though I can catch the event when submit button is clicked, I am NOT able to catch the event when the button2 is clicked. Specific to the sample code below, I tried using $query->param('resetButton/button/Reset') but it gets nothing in return and the variable I am using to store the value (my $buttonStatus) is always blank.

Can you please help me here by suggesting I way to catch the event when the button2 is clicked.

I have written a small test code (given below) just to demonstrate what I am trying do:

#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; my $query = new CGI; print $query->header; my $formStatus = $query->param('submitButton'); my $buttonStatus = $query->param('resetButton'); print "Value of \$formStatus is: $formStatus". localtime()."\n"; print "Value of \$buttonStatus is: $buttonStatus". localtime()."\n"; print "<html>\n"; print "<head>\n"; print "</head>\n"; print "<body>\n"; print "<form id=\"form1\" method=\"post\">\n"; print "<input type=\"submit\" name=\"submitButton\" value=\"Submit +\" id=\"button1\"/>\n"; print"<button type=\"button\" name=\"resetButton\" onclick=\"reset +tingFunction()\" id=\"button2\">Reset</button>\n"; print "</form>\n"; print "</body>\n"; print "</html>\n";

What I want is to be able to determine when button2 is clicked.

Replies are listed 'Best First'.
Re: How to catch event when button (which is NOT submit button) is clicked.
by GotToBTru (Prior) on Nov 11, 2015 at 21:45 UTC

    First step would be to write the HTML that would do what you want, and then write the perl to create that HTML. To tie events (such as your db query) to buttons is going to involve javascript, most likely.

    Dum Spiro Spero
Re: How to catch event when button (which is NOT submit button) is clicked.
by Anonymous Monk on Nov 11, 2015 at 22:02 UTC
[Solved]: How to catch event when button (which is NOT submit button) is clicked.
by Perl300 (Friar) on Nov 11, 2015 at 23:29 UTC
    As suggested by choroba, I used two submit buttons and that got me what I wanted to do. Code is something like:
    #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); use Data::Dumper; use CGI qw/:standard/; my $query = new CGI; print $query->header; my $formStatus = $query->param('submitButton'); my $buttonStatus = $query->param('resetButton'); print "Value of \$formStatus is: $formStatus". localtime()."\n"; print "Value of \$buttonStatus is: $buttonStatus". localtime()."\n"; print "<html>\n"; print "<head>\n"; print "</head>\n"; print "<body>\n"; print "<form id=\"form1\" method=\"post\" class=\"admin-form\">\n" +; print "<input type=\"submit\" name=\"submitButton\" value=\"Submit +\" id=\"button1\"/>\n"; print"<input type=\"submit\" name=\"resetButton\" value=\"Reset\" +id=\"button2\"/>\n"; print "</form>\n"; print "</body>\n"; print "</html>\n";
      That's not exactly what I suggested. The submit buttons have different names, but it's useless - you can never press both of them at the same time. Give them the same name and a different value, then you'll have to only check one parameter.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thanks again, choroba. Yes having same name and different values would need checking only one parameter value (which I was already doing for submitButton). At present I am checking two different parameters one for submitButton and second for resetButton (and I don't need to click both at same time). Though it also gives me what I wanted to do but your suggestion will make it more efficient.