Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Calling a sub from a button

by gryphon (Abbot)
on Oct 25, 2002 at 21:48 UTC ( [id://208156]=note: print w/replies, xml ) Need Help??


in reply to Calling a sub from a button

Greetings cal,

Fortunately (from a security perspective) you can't just run Perl subs via HTML. You'll need to pass your action request via HTTP to your CGI that will interpret it and run the function that you desire.

The HTML might look something like:

<FORM action="yourscript.cgi" method=post> <INPUT type=hidden name=action value="storeevent"> ...

Then you're Perl might something look like:

#!/usr/bin/perl -wT use strict; use CGI; $CGI::DISABLE_UPLOADS = 1; $CGI::POST_MAX = 1024; my $cgi = new CGI; my $action = $1 if $cgi->param('action') =~ /^(\w+)+/; &store_event if ($action eq 'storeevent');

Whatever you do, don't be tempted to do something like:

my $action = $cgi->param('action'); &$action;

UPDATE: The above assumes two things: first that you'll print out the HTTP header elsewhere in your script before sending HTML, and second that the "action" CGI parameter is always defined. If it's not, then you'll get a warning. In such cases, you could use:

my $action = (defined $cgi->param('action')) ? ($cgi->param('action') =~ /^(\w+)+/) ? $1 : '' : '';

This just makes sure that the parameter is defined. If it isn't or if it is and contains badness, then $action results in an empty string.

gryphon
code('Perl') || die;

Replies are listed 'Best First'.
Re: Re: Calling a sub from a button
by cal (Beadle) on Oct 25, 2002 at 22:08 UTC
    I'm sorry, I thought I mentioned that the call was from a script. I should have said a CGI script.
      In that case you would need the form action to be the script, and submit another field which the script uses to find the right subroutine. You could use a hidden field. But a nice way to do it IMHO is with the submit button. Since the value field in a Submit button only gets submitted with the form when that particular button is pressed, you have the option of different buttons within the same form. For example, in the html:
      <FORM ACTION="yourscript.pl" METHOD="POST"> <!-- insert other form inputs here --> <INPUT TYPE="SUBMIT" NAME="Action" VALUE="Run Foo"> <INPUT TYPE="SUBMIT" NAME="Action" VALUE="Run Bar"> <INPUT TYPE="SUBMIT" NAME="Action" VALUE="Store Event"> </FORM>
      Then in the script (in this example saved as yourscript.pl) you could have something like
      use CGI qw/:standard/; if (param('Action') and param('Action') eq 'Run Foo') { foo(); } elsif (param('Action') and param('Action') eq 'Run Bar') { bar(); } elsif (param('Action') and param('Action') eq 'Store Event') { store_event(); } else { print header, start_html('Ack!'), h1 'you have to call this from the form!', end_html; }


      § George Sherston

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://208156]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found