Click to See Complete Forum and Search --> : HTML form values not passing to perl cgi


Talonsrest
09-01-2006, 01:07 PM
I am trying to build a form that e-mails the data it collects to some address. I've had the HTML forum look at the page and they directed me to this forum to have the scripts looked at as they don't see anything wrong with the form itself.

I've tried a number of different ways to create this script but with no success. The tests on the web page all work fine. If I test each script using command lines, they work fine as well. If I put the two parts together, the page will launch the script but the script sends a formatted e-mail but with blanks where the values should be.

servmailer.pl v3.0
#!/usr/local/bin/perl

use Net::SMTP;

read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split(/=/,$item,2);
$content=~tr/+/ /;
$content=~s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}

# Build the e-mail body
$subject = "Subject: New Server Request \n";
$newline = "\n";
$line1 = " would like to request a server\n";
$line2 = "Ram = ";
$line3 = "\nHard Drive =";

@message = ($subject, $newline, $fields{name}, $line1, $line2, $fields{ram} , $line3, $fields{harddrive});


# Email the form results
$smtp = Net::SMTP->new("conference.co.marin.ca.us");
$smtp->mail("mwarden\@co.marin.ca.us");
$smtp->to("mwarden\@co.marin.ca.us");
$smtp->data(@message);
$smtp->quit;

servmailer.pl v2.0
#!/usr/local/bin/perl

use CGI;
use Net::SMTP;

# Create the CGI object
my $query = new CGI;

# Output the HTTP header
print $query->header ( );

# Capture the form results
my $name = $query->param("name");
my $ram = $query->param("ram");
my $harddrive = $query->param("harddrive");

# Build the e-mail body
$subject = "Subject: New Server Request \n";
$newline = "\n";
$line1 = " would like to request a server\n";
$line2 = "Ram = ";
$line3 = "\nHard Drive =";
@message = ($subject, $newline, $name, $line1, $line2, $ram, $line3, $harddrive);

# Email the form results
$smtp = Net::SMTP->new("conference.co.marin.ca.us");
$smtp->mail("mwarden\@co.marin.ca.us");
$smtp->to("mwarden\@co.marin.ca.us");
$smtp->data(@message);
$smtp->quit;

servmailer.pl v1.0
#! /usr/local/bin/perl -w

use Net::SMTP;
use CGI qw(:standard);

# Build the message
$subject = "Subject: New Server Request \n";
$newline = "\n";
$line1 = " would like to request a server\n";
$line2 = "Ram = ";
$line3 = "\nHard Drive =";
@message = ($subject, $newline, param('name'), $line1, $line2, param('ram'), $line3, param('harddrive'));

# E-mail the message
$smtp = Net::SMTP->new("conference.co.marin.ca.us");
$smtp->mail("mwarden\@co.marin.ca.us");
$smtp->to("mwarden\@co.marin.ca.us");
$smtp->data(@message);
$smtp->quit;

I built this script to just dump the values passed to it to a file to try to figure out what was going on.

Dump2file.pl
#! /usr/local/bin/perl -w

use Net::SMTP;
use CGI qw(:standard);

$myfile = 'C:\temp\test.txt';
$errfile = 'c:\temp\err.txt';

open(FILE, ">$myfile") or die "cant find file\n";
open STDERR, ">$errfile";

print FILE param('name'), "\n";
print FILE param('ram'), "\n";
print FILE param('harddrive');

close FILE;

In all cases, the script does execute, but the results are as if no information is being passed to the scripts.

I am creating this on a XP professional machine. I've tried it with IIS installed. I removed IIS and tried it with Apache installed just to see if it was the web engine. Could it be something with XP passing the information incorrectly?

I'm stumped, Anyone?

Here is the web page in case anyone wants to test it on their own.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head><title>Test Server Form</title></head>
<body>
<form action="servmailer.pl" method="get" >
<p>
<h4>Name:</h4>
<input name='name' class="required" />
<h4>Memory</h4>
</p>
<p>
<SELECT name='ram' class="required">
<option selected value=128>128 Meg</option>
<option value=256>256 Meg</option>
<option value=512>512 Meg</option>
<option value=1024>1 Gig</option>
</select>
</p>
<p>
Hard Drive:
<select name='harddrive' class="required">
<option selected value=18>18 Gig</option>
<option value=40>40 Gig</option>
<option value=128>80 Gig</option>
<option value=128>120 Gig</option>
</select>
</p>
<p>
<input type="submit" value="Send" />
</p>
</form>
</body>
</html>

Nedals
09-01-2006, 01:43 PM
#!/usr/local/bin/perl
use strict; ## add this
use CGI;
use Net::SMTP;

# Create the CGI object
my $query = new CGI;

### Output the HTTP header
##print $query->header(); #### Why? You are not outputting HTML. (and no spaces)

# Capture the form results
my $name = $query->param('name'); ## use single quotes
my $ram = $query->param('ram');
my $harddrive = $query->param('harddrive');

# Build the e-mail body
#$subject = "Subject: New Server Request \n";
#$newline = "\n";
#$line1 = " would like to request a server\n";
#$line2 = "Ram = ";
#$line3 = "\nHard Drive =";
#@message = ($subject, $newline, $name, $line1, $line2, $ram, $line3, $harddrive); # Why??

## How about this instead
my $message = <<MESSAGE;
Subject: New Server Request
$name would like to request a server
Ram = $ram
Hard Drive = $harddrive
MESSAGE
(NOTE: 'MESSAGE' MUST be on a line by itself - no spaces allowed before or after)

# Email the form results
$smtp = Net::SMTP->new("conference.co.marin.ca.us");
$smtp->mail("mwarden\@co.marin.ca.us");
$smtp->to("mwarden\@co.marin.ca.us");
#$smtp->data(@message); ### Where is the data subroutine ???
$smtp->$message; ## Do it this way instead.
$smtp->quit;

Talonsrest
09-01-2006, 06:15 PM
I've tested the changes from a command line and I am getting the following errors.

Global symbol "$smtp" requires explicit package name at C:\Code\Srvwebform\servmailer3.pl line 34.
Global symbol "$smtp" requires explicit package name at C:\Code\Srvwebform\servmailer3.pl line 35.
Global symbol "$smtp" requires explicit package name at C:\Code\Srvwebform\servmailer3.pl line 36.
Global symbol "$smtp" requires explicit package name at C:\Code\Srvwebform\servmailer3.pl line 38.
Global symbol "$smtp" requires explicit package name at C:\Code\Srvwebform\servmailer3.pl line 39.
Execution of C:\Code\Srvwebform\servmailer3.pl aborted due to compilation errors
.
'ram' is not recognized as an internal or external command,
operable program or batch file.
'harddrive' is not recognized as an internal or external command,
operable program or batch file.

I know I was getting the explicit errors with my code before so I removed the use strict; from my script.

I cleaned up the comments in the script (thanks, I am keeping a copy of the one with your comments to learn from :) ) Here is the new script.

#!/usr/local/bin/perl
use strict;
use CGI;
use Net::SMTP;

# Create the CGI object
my $query = new CGI;

# Capture the form results
my $name = $query->param('name');
my $ram = $query->param('ram');
my $harddrive = $query->param('harddrive');

# Build message
my $message = <<MESSAGE;
Subject: New Server Request
$name would like to request a server
Ram = $ram
Hard Drive = $harddrive
MESSAGE

# Email the form results
$smtp = Net::SMTP->new("conference.co.marin.ca.us");
$smtp->mail("mwarden\@co.marin.ca.us");
$smtp->to("mwarden\@co.marin.ca.us");
$smtp->$message;
$smtp->quit;

Nedals
09-01-2006, 07:14 PM
When using 'strict', ALL variables MUST be declared using 'my'
(or 'our' once you start using modules and wish to declare global variables - but that's another topic)

Edit to:
my $smtp = Net::SMTP->new("conference.co.marin.ca.us");
and all those 'Global symbol .... ' errors will go away. So now you know what to do if you see that error message in the future. :)

The other errors MAY be a result of that missing 'my'.