Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

html form, action pointing to a subroutine? 1

Status
Not open for further replies.

martinasv

Technical User
Joined
Feb 12, 2006
Messages
24
Location
HR
Is it possible to write HTML form in perl so that the action points to a subroutine in the same script?

E.g.

print "<form action=\"do_something()\">";

where do_something is a subroutine?
 
I'm not sure I understand what you're looking for. If you want to run a subroutine to determine the end result action text when your HTML form is printed, then sure.

Code:
sub do_something() {
#code here
}

my $action = do_something();

print qq(<form action="$action">);

If you mean to have the form tag to literally be 'do_something()', then it's possible, but only if that routine was loaded into the HTML page and the user can run PerlScript from their computer.

Since HTML is not a "always connected" solution, you wouldn't be able to submit form data directly into a script. The Perl script would have run on the server, and sent the output to the user's browser and ended the connection. When a user submits a form, a completely new request comes into the web server and is processed. Only by using technology such as cookies would you know that it's the same person returning.

- George
 
Tnx!

The first option is what I wanted. :-)

But I'm new to all these stuff, so I'm constantly posting questions on this forum.
 
you can do this with a hash table and references:

Code:
<form action="myscript.pl">
<input type=hidden name=com value=[b]3[/b]>
<input type=text name=qty>
<input type=submit>
</form>

in the script:

my %coms = (
  1 => \&foo,
  2 => \&bar,
  [b]3[/b] => \&order
);

my $command = param('com') || 0;
my $qty = param('qty') || 0;
$coms{$command}->($qty);

sub order {
   my $parameter = shift
   blah blah
}
 
Something close to what he originally wanted is to just use a JavaScript function.

Code:
<script>
function do_something() {
   window.alert ('doing something?');
   return true;
}
</script>

<!-- Set an onSubmit function in your form -->
<form onSubmit="do_something()" action="script.pl">
<!-- form contents -->

So when the form is submitted, the JavaScript function do_something() is called just before submitting. If the function returns true, the form is passed on to the action (script.pl). If the function returns false, the submit is canceled (you could put a form validator to only submit the form if they filled out a text field or something)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top