PDA

View Full Version : Passing submit cariable to Perl


id10error
03-19-2005, 10:18 PM
Hi:

I am going crazy here trying to figure out how to carry a variable from one page to the other. The first page, called

"return01.htm" has form properties with 1 hidden input text field called "sumthing". Upon clicking the submit button, open

a PERL page called "record01.cgi" and display the variable "sumthing". Can anyone help me with this please?

Here is the code for record01.cgi:


**************************************************************************************************** *****
#! /usr/bin/perl
print "Content-type: text/html", "\n\n";

read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});

print <<'EOF';

<html>
<head>
<title>Variable Carried-Over Page</title>
<link href="My-Text.css" type="text/css" rel="stylesheet">

<style>
<!--
.stdSmall {
COLOR: #962a2a
}
.stdSmall {
FONT-SIZE: x-small; FONT-FAMILY: verdana
}
.stdSmall {
FONT-SIZE: xx-small
}
-->
</style>

</head>

EOF

$ip=$ENV{'REMOTE_ADDR'};

print <<EOF;

<body background="Back01.jpg">

<table border="1" width="959" id="Table001" bordercolor="#FFFFFF" cellspacing="1">
<tr>
<td>
<div align="center">
<table border="1" width="812" id="Table002" bordercolor="#800000" cellspacing="0" cellpadding="0">
<tr>
<td width="56%" height="24">&nbsp;</td>
</tr>
<td width="56%">
<table border="1" width="100%" id="Table003" bordercolor="#00FF00" cellspacing="1">
<tr>
<td width="1%">&nbsp;</td>
<td width="98%">
<table border="1" width="100%" id="Table004" bordercolor="#FF0000" cellspacing="1">
<tr>
<td width="100%">&nbsp;</td>
</tr>
<form name="Info01" method="post" action="/cgi-bin/nextpage.cgi">
<tr>
<td width="100%">
<table border="1" width="100%" id="Table005" bordercolor="#9999FF" cellspacing="1">
<tr>
<td width="100%" bgcolor="#333333">&nbsp;</td>
</tr>
<tr>
<td width="100%" bgcolor="#333333">
<table border="1" width="100%" id="Table006" bordercolor="#0000FF" cellspacing="1">
<tr>
<td width="1%">&nbsp;</td>
<td width="80%"><b><font size="2" face="Arial" color="#FFFF00">Information:</font></b></td>
<td width="19%"><input id="total5" type="text" size="18" name=$sumthing></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="100%" bgcolor="#333333">
<table border="1" width="100%" id="Table007" bordercolor="#0000FF" cellspacing="1">
<tr>
<td width="1%">&nbsp;</td>
<td width="99%">
<span class="stdSmall"><font color="#FFFF00">The following variable was carried over from the previous page.&nbsp; This
page is in PERL and called return01.cgi under the correct directory.</font></span></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="100%">&nbsp;</td>
</tr>
</form>
</table>
</td>
<td width="1%">&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</body>
</html>
EOF

mlseim
03-19-2005, 10:28 PM
Try replacing these lines:
=========================================
#! /usr/bin/perl
print "Content-type: text/html", "\n\n";

read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});



...with these lines:
=========================================
#! /usr/bin/perl

use CGI ':standard';
my $sumthing = param('sumthing');

print "Content-type: text/html", "\n\n";


... or depending on your version of Perl and the CGI Module,
you might try this variation:
=========================================
#! /usr/bin/perl
use CGI;

my $query = new CGI;

my $sumthing = $query->param("sumthing");
my $email = $query->param("email");
print "Content-type: text/html", "\n\n";




#
message to other Perl programmers:

Am I correct about the variations as they pertain to the
version of Perl and the CGI Module? Which one is considered
most acceptable? ... or are they both valid? I always wondered
this, as both methods work with my webhost.
..thanks,
--max--

rwedge
03-20-2005, 01:53 AM
On the two styles of using the CGI module, object-oriented and function-oriented.

from the CGI.pm docs :
DESCRIPTION
PROGRAMMING STYLE

There are two styles of programming with CGI.pm, an object-oriented
style and a function-oriented style. In the object-oriented style you
create one or more CGI objects and then use object methods to create the
various elements of the page. Each CGI object starts out with the list
of named parameters that were passed to your CGI script by the server.
You can modify the objects, save them to a file or database and recreate
them. Because each object corresponds to the "state" of the CGI script,
and because each object's parameter list is independent of the others,
this allows you to save the state of the script and restore it later.

For example, using the object oriented style, here is how you create a
simple "Hello World" HTML page:

#!/usr/local/bin/perl -w
use CGI; # load CGI routines
$q = new CGI; # create new CGI object
print $q->header, # create the HTTP header
$q->start_html('hello world'), # start the HTML
$q->h1('hello world'), # level 1 header
$q->end_html; # end the HTML

In the function-oriented style, there is one default CGI object that you
rarely deal with directly. Instead you just call functions to retrieve
CGI parameters, create HTML tags, manage cookies, and so on. This
provides you with a cleaner programming interface, but limits you to
using one CGI object at a time. The following example prints the same
page, but uses the function-oriented interface. The main differences are
that we now need to import a set of functions into our name space
(usually the "standard" functions), and we don't need to create the CGI
object.

#!/usr/local/bin/perl
use CGI qw/:standard/; # load standard CGI routines
print header, # create the HTTP header
start_html('hello world'), # start the HTML
h1('hello world'), # level 1 header
end_html; # end the HTML


/Bob

mlseim
03-20-2005, 02:19 AM
That's sort of interesting.

I've only used the "use CGI" for
bringing in variables from a form
and for cookie stuff. So I guess
I'm sort of mixing various methods.

But I like using CGI instead of
parsing my own variables from
a form. I'm guessing CGI.pm has
pretty good security features,
parsing strings better than I can
do myself.

Thanks for the info. I guess I'll
have to do some more reading on
this, as I don't fully understand
objects.