View Full Version : Cannot pass form values to CGI
Talonsrest
08-31-2006, 11:05 PM
I am trying to set up a simple form that passes the values in the form to a CGI script that e-mails the information. If it test the form with no action and the method set to "get", the form displays the values just fine in the URL. I've tested the CGI script with a command line and it works as well.
When I put the action back into the form, it runs the script but does not pass the values. I'm testing this on a Windows XP Professional machine with IIS installed. Is there a problem with building and testing the page and scripts this way?
Here is the code for the page
I hope someone sees something I don't.
Thanks in advance
<!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="post" >
<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>
rafiki
09-01-2006, 02:08 AM
wat code is in your servmailer.pl ?
Talonsrest
09-01-2006, 06:34 PM
I have tried several different scripts with this.
Servermailer.pl 3.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);
$subject = "Subject: New Server Request";
$newline = "\n";
$requester = param('name'). " would like to request a server\n";
$ramsize = "Ram = ". param('ram');
$drivesize = "\nHard Drive =". param('harddrive');
@message = ($subject, $newline, $requester, $ramsize, $drivesize);
$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;
This script I tried just to see if any data was being passed.
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 runs but there is no data that comes through.
FishMonger
09-01-2006, 08:37 PM
Your description of the problem is a little unclear and some parts can't be true.In all cases, the script runs but there is no data that comes through. Are you sure of that?
If you leave out the action attribute, there is no way for the web server to know which script is needed to process the form, so it should return an error page not the form submission values. You can only leave out the action attribute when the form is part of the script that processes the form submission (self refering form/script).
When using the action attribute, how do you know that the form is calling the script? With the exception of Servmailer.pl v2.0 sending the html header, no output is being sent to the browser in any of the code samples that you posted.
I suspect that the scripts work correctly when executed from the command line, but they are not being executed when the form is submitted via the web page.
Have you checked the IIS config to make sure that the directory where the scripts are located is configured to run scripts? Are the scripts in the same directory as the html form? If so, you should move them to a different directory and make sure the path in the action attribute is pointing to the proper location.
Talonsrest, the Servermailer.pl 3.0 code you provided will only receive information sent to it with method="POST"
To also receive method="GET", you might insert 2 lines above your read(...) line and wrap the read() line in an else{}:
my $temp = '';
if ($ENV{'REQUEST_METHOD'} eq 'GET') { $temp = $ENV{QUERY_STRING}; }
else { read(STDIN,$temp,$ENV{'CONTENT_LENGTH'}); }
@pairs=split(/&/,$temp);
To see what is actually being received, this temporary debugging line could be put somewhere below your information parsing for() loop:
print "Content-type: text/plain\n\n".join("\n",%fields);
Looks like Servmailer.pl v2.0 should receive information sent to it with either GET or POST. It might help to put a similar line into that one, just to see:
print "Content-type: text/plain\n\n$name\n$ram\n$harddrive";
Talonsrest
09-06-2006, 08:35 PM
If you leave out the action attribute, there is no way for the web server to know which script is needed to process the form, so it should return an error page not the form submission values.
According to the references that I have been using, by leaving off the action attribute, the form is just submitted to the current URL. If the page is one that is built by a script, then it just reprocesses the current page. I imagion it's like an overloaded operation. The page will do different things depending on the parameters it receives.
When using the action attribute, how do you know that the form is calling the script? With the exception of Servmailer.pl v2.0 sending the html header, no output is being sent to the browser in any of the code samples that you posted.
Because an e-mail is being generated. I receive an email to my account that has the following information.
would like to request a server
Ram =
Hard Drive =
Because this is being generated I know that the script is working. Also I created dump2file.pl which just takes whatever arguments are passed to it and writes them to the file C:\temp\test.txt. If I run the form with that as the action. File test.txt is created if it isn't there, but it is empty.
Have you checked the IIS config to make sure that the directory where the scripts are located is configured to run scripts? Are the scripts in the same directory as the html form? If so, you should move them to a different directory and make sure the path in the action attribute is pointing to the proper location.
I believe that I have them in the correct location. I'll take a look at this and make sure.
Thanks for this input. Everything helps.
Talonsrest
09-06-2006, 10:35 PM
To see what is actually being received, this temporary debugging line could be put somewhere below your information parsing for() loop:
print "Content-type: text/plain\n\n".join("\n",%fields);
I've tried all of the scripts both with "get" and with "post" and still only receive an e-mail message with all the formating but none of the data.
When I put the debug line in the results are:
Content-type: text/plain
Looks like Servmailer.pl v2.0 should receive information sent to it with either GET or POST. It might help to put a similar line into that one, just to see:
print "Content-type: text/plain\n\n$name\n$ram\n$harddrive";
Results for this was the same
Content-type: text/plain
<form action="servmailer.pl" method="post" >
If I understand correctly, the form submits method POST to a script located in the same directory as the form. The script then sends an email but no information submitted with the form is included with the email.
That would seem to indicate there is a problem somewhere between the submission of the form and the sending of the email.
Things to check:
1.
Verify the directory where the form and script are located will accept POST information.
2.
Try both "www.domain.com" and "domain.com" versions of URL when loading the form into your browser. The hosting company might be doing a URL rewrite from one version to the other, and the rewrite might be dropping the POST information.
3.
Server might be set up to work differently with .pl files than with .cgi files -- try .cgi just to see.
4.
Hosting company might be sandboxing CGI scripts in a way that strips POST information.
The following script can test whether or not anything is actually received. It will accept information that arrives method GET or POST and print whatever it receives in the browser.
#!/usr/bin/perl
use strict;
my $fields = '';
if ($ENV{'REQUEST_METHOD'} eq 'GET') { $fields = $ENV{QUERY_STRING}; }
else { read(STDIN,$fields,$ENV{'CONTENT_LENGTH'}); }
print "Content-type: text/plain\n\n".join("\n",split(/&/,$fields));
5.
If the form information is received by your form processing script, then the script isn't properly generating the email that it sends out.
Those are some areas it might be breaking at. Each needs to be checked until you find the problem. If none of the above are the problem, then it is something else. Things don't just not work. There is always a reason.
FishMonger
09-07-2006, 03:15 AM
There still seams to be some confusion as to your setup. Based on the code you posted, it appears that you have a separate html file that holds your form and you have several versions of the script that processes the form and sends the email. However, you have said that if you leave out the action attribute, the script executes and sends the email but only displays the form values if the methods is set to GET. Without setting the action attribute, it becomes a self referring script, but since a plain html file can't process the form, I can only conclude that the form must be part of the script instead of being a separate html file.
If it test the form with no action and the method set to "get", the form displays the values just fine in the URL.
Does each version display the form values if the method is set to get, or is Servermailer.pl 3.0 the only version that displays the values?
Does an email get sent when you first load the form page or only after filling in and submitting the form?
Have you checked the web server error logs to see if there is any clues to the problem?
Can you post the complete scripts (as attachments) so we can see exactly what you're doing?
rwedge
09-07-2006, 05:24 AM
You could use this to display what your perl script catches.#!/usr/bin/perl -w
use CGI qw(:standard);
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;
print "$content<br />";
$fields{$key}=$content;
}
$line2 = "Ram = ";
$line3 = "\nHard Drive =";
print header,start_html(-title =>'Mail Test'),p;
print "$fields{name}<br /> $line2 $fields{ram}<br /> $line3 $fields{harddrive}";
print end_html;I would suspect the handling of the @pairs with your foreach loop if you do not see the values.
Talonsrest
09-07-2006, 11:29 PM
1.
Verify the directory where the form and script are located will accept POST information.
3.
Server might be set up to work differently with .pl files than with .cgi files -- try .cgi just to see.
This may be where the problem is. I have been accessing the page directly through the browser. To test your suggestions I set up a local host entry and tried to access the page through a WWW address. After a number of errors and configuration changes to the IIS service, I am now getting the following error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
cant find file
It seems that I may have something wrong with my web server settings. I'm going to see what I can come up with from this angle and go from there.
Thanks for the help. I may not have solve this yet but I'm learning a lot
=)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.