PDA

View Full Version : CGI Mail form isn't displaying all the values in the results email...


Errica
08-31-2008, 06:59 AM
The problem is in the results that are emailed (below). I have a set of checkboxes with the name "Service". If someone selects multiple checkboxes, only the first is displayed in the email results. This isn't an issue in the log (also below). I've reached an impasse...very frustrating. :(

Please keep in mind we're restricted at this time so switching to a php form isn't an option. Is there an oversight in the code somewhere?

Here's the script:
#!/usr/bin/perl

use POSIX qw(strftime);
use CGI;
use CGI::Carp qw(fatalsToBrowser);

$Recipient = "info\@mydomain.com";
$From = "auto-reply\@mydomain.com";
$Subject = " . mydomain.com - Web Request .";
$Web_Address = "http://www.mydomain.com";
$Return_Link = "http://www.mydomain.com/thanks.php";
$Send_Reply = "1";
$Reply_Template = "fp.txt";
$Log_Forms = "1";

$Mail_Program = "/usr/sbin/sendmail -t";

@fields = ('Name','Company','Email','Phone','Address','URL','Service','Request','Contact_Method');
@friendly_fields = ('Name','Company','Email','Phone','Address','Web Address','Service','Request','Contact Method');

my $cgi = new CGI;
my %FORM = $cgi->Vars;
my $date = strftime("%B %d, %Y", localtime);
my $time = strftime("%I:%M:%S %p", localtime);

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

open(MAIL,"|$Mail_Program") || &noprogram;

print MAIL "To: $Recipient\n";

if (! $FORM{'Email'}) {
print MAIL "From: $Recipient\n";
} else {
print MAIL "From: $FORM{'Email'}\n";
}

if (! $FORM{'subject'}) {
print MAIL "Subject: $Subject\n\n";
} else {
print MAIL "Subject: $FORM{'subject'}\n\n";
}

print MAIL "Submitted at $time on $date...\n\n";

for $idx (0..$#fields ) {
$field_data = $FORM{$fields[$idx]};
$friendly_field = $friendly_fields[$idx];
if ($field_data) {
if ($fields[$idx] eq "main") {
$field_data =~ s/QnE/n/g;
print MAIL "$friendly_field: $field_data\n\n";
} else {
print MAIL "$friendly_field: $field_data\n\n";
}
}
}
close(MAIL);

if ($Send_Reply eq "1") {
open(REPLY,"$Reply_Template");
@reply = <REPLY>;
close(REPLY);

foreach $line(@reply) {
$line =~ s/QnE/n/g;
$fiction .= $line;
}

open(MAIL,"|$Mail_Program") || &noprogram;
print MAIL "To: $FORM{'Email'}\n";
print MAIL "From: $From\n";
print MAIL "$fiction\n\n";
print MAIL "\n\n\nNote: This message was not sent unsolicited. It was sent through a form located at $Web_Address. If you believe this message was received on error, please disregard it.";
close(MAIL);
}


if ($Log_Forms eq "1") {
if ($FORM{'Email'}) {
open(LOG,">>submitted/$date.txt");
print LOG "Submitted at $time on $date...\n\n";
foreach $field(@fields) {
if ($FORM{$field}) {
if ($field eq "main") {
$FORM{'main'} =~ s/QnE/n/g;
print LOG "$field = $FORM{'main'}\n";
} else {
print LOG "<p>$field = $FORM{$field}\n";
}
}
}
print LOG "\n";
print LOG "---------------------------\n\n";
close(LOG);
}
}

print qq~
<HTML>
<HEAD>
<META HTTP-EQUIV=Refresh CONTENT=0;URL="$Return_Link">
</HEAD>

<BODY></BODY>
</HTML>
~;
exit;

sub noprogram {
print qq~
Sendmail program failed to open!<br>
Message NOT sent.<br>
Please contact the webmaster about this problem.
~;
exit;
}


Here's the log, which is correct (notice a successful entry HERE after Custom Design |):
Submitted at 01:38:06 AM on August 31, 2008...

<p>Name = Errica
<p>Company = My Company
<p>Email = info@mycompany.org
<p>Phone = 555-555-5555
<p>Address = 123 Main
<p>URL = www.mycompany.org
<p>Service = Custom Design | Redesign |
<p>Request = test
<p>Contact_Method = Phone

---------------------------

Here's the page with the form:
<label for="Custom Design" style="cursor: pointer"><input id="Custom Design" class="prodchex" type="checkbox" name="Service" value="Custom Design | ">Custom Web Design</label><br>

<label for="Redesign" style="cursor: pointer"><input id="Redesign" class="prodchex" type="checkbox" name="Service" value="Redesign | ">Redesign</label><br>

<label for="Ecommerce" style="cursor: pointer"><input id="Ecommerce" class="prodchex" type="checkbox" name="Service" value="Ecommerce | ">Ecommerce</label><br>

And here's the email results (notice how there is nothing after Custom Design |):
Submitted at 01:38:06 AM on August 31, 2008...

Name: Errica

Company: My Company

Email: info@mycompany.org

Phone: 555-555-5555

Address: 123 Main

Web Address: www.mycompany.org

Service: Custom Design |

Request: test

Contact Method: Phone

FishMonger
08-31-2008, 07:51 AM
I guess you missed this section of the CGI documentation.a hash cannot distinguish between scalar and list context, multivalued parameters will be returned as a packed string, separated by the "\0" (null) character. You must split this packed string in order to get at the individual values.

http://search.cpan.org/~lds/CGI.pm-3.41/CGI.pm#FETCHING_THE_PARAMETER_LIST_AS_A_HASH:

<label for="Custom Design" style="cursor: pointer"><input id="Custom Design" class="prodchex" type="checkbox" name="Service" value="Custom Design">Custom Web Design</label><br>

<label for="Redesign" style="cursor: pointer"><input id="Redesign" class="prodchex" type="checkbox" name="Service" value="Redesign">Redesign</label><br>

<label for="Ecommerce" style="cursor: pointer"><input id="Ecommerce" class="prodchex" type="checkbox" name="Service" value="Ecommerce">Ecommerce</label><br>

for $idx (0..$#fields ) {
$field_data = $FORM{$fields[$idx]};
$friendly_field = $friendly_fields[$idx];
if ($field_data) {
if ($fields[$idx] eq "main") {
$field_data =~ s/QnE/n/g;
print MAIL "$friendly_field: $field_data\n\n";
} else {
if($fields[$idx] eq "Service") {
$field_data = join(" | ", split("\0", $field_data));
}

print MAIL "$friendly_field: $field_data\n\n";
}
}
}

Errica
08-31-2008, 08:29 AM
You're right and thank you...worked just fine!!

Errica