CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Calling subroutines to parse forms

 



d1zz13
User

Aug 30, 2000, 12:27 AM

Post #1 of 2 (485 views)
Calling subroutines to parse forms Can't Post

Good Morning all,

I wonder if anyone out there could help? I've created a script that prints an html form, then when I want to parse the form I call a subroutine, but it just doesn't appear to work.

I've added some very simple code here to display what I've got, is it difficult or am I missing something simple?

Here's the code, and you can see it in action here.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>



#!/usr/bin/perl

# Call subroutine print_html
&print_html;

# Specifies the subroutine print_html
sub print_html{
print "Content-type: text/html\n\n";
print qq{
<html>
<head>
<title>Test Form Script</title>
</head>
<body bgcolor="#000000" text="#FFFFFF">
<form method="post" action="?get_info">
Select an option<br>
<input type="radio" name="select" value="Yes">
<input type="radio" name="select" value="No"><br>
<input type="Submit" name="Submit" value="Submit">
</form>
</body>
</html>
}
}

# Specifies the subroutine get_info
sub get_info{
use CGI;
$form=newCGI;
$results=$form->param('select');
&check_it;
}

# Specifies the subroutine check_it
sub check_it{
print "Content-type: text/html\n\n";
print "You selected $results";
}
</pre><HR></BLOCKQUOTE>

Regards and TIA
Richard Smile


------------------
Dizzie Scripts


japhy
Enthusiast

Aug 30, 2000, 4:19 AM

Post #2 of 2 (485 views)
Re: Calling subroutines to parse forms [In reply to] Can't Post

You can't just send a parameter to a CGI program and expect it to run a function of that name for you.

Oddly enough, this question came up in the CIWAC (comp.infosystems.www.authoring.cgi) newsgroup, and I had an answer for it. If you call your program as http://www.server.com/program.cgi?FUNCNAME, then the following subroutine will execute the given function (if it exists).

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# dispatch(CONTINUE)
# CONTINUE - boolean determining whether to
# continue running the program after the
# function has been called
sub dispatch {
my $continue = shift;
my $func = $ENV{QUERY_STRING};
my $glob = *$func;
$glob->() if defined &$glob;
exit unless $continue;
}
</pre><HR></BLOCKQUOTE>

This function is used like so:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

dispatch($x); # where $x is true or false
# your program here...
sub dispatch {
# the dispatch function here...
}
</pre><HR></BLOCKQUOTE>

Note that dispatch() makes no effort to URL-decode the query string. Should that be needed, you can do that yourself.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives