PDA

View Full Version : Trying to parse a simple Form


roadrunnerray
12-10-2007, 06:26 PM
I created a Pearl script about 3-years ago and have not done anything with Pearl since then. My usually foggy mind is more so after this extended period.
I recently modified this script and am having a problem.

The Pearl script executes OK but the data I print to the browser is not correct.

The data printed to browser is displayed below (note the Name prints correctly but the Title field appears to print the *key* [title for the second field right after the data for the 1st field]). The remaining form fields do not print….
---------------------------------------------------------------------------------------
Name: John Doe title
Title:
Email Address:
Daytime Telephone:
Affiliation:
I Will Attend:
Lodging:
Dinner:
Special Food Requirements:
---------------------------------------------------------------------------------------

Here is the HTML Form:
---------------------------------------------------------------------------------------
<form action="http://128.123.83.183/cgi-bin/ADVANCERegForm.pl" method="post" enctype="text/plain">

<p><strong> Name (First/Last)</strong><br />
<input type="text" class="formField" name="fullname" size="60"></input></p>

<p><strong>Title</strong><br />
<input type="text" class="formField" name="title" size="60"></input></p>

<p><strong>Email Address</strong><br />
<input type="text" class="formField" name="email" size="60"></input></p>

<p><strong>Daytime Telephone (With Area Code)</strong><br />
<input type="text" class="formField" name="phone" size="60"></input></p>

<p><strong>Affiliation:</strong>
<select name="affiliation">
<option selected="selected">LANL</option>
<option>NMSU</option>
<option>NMT</option>
<option>UNM</option>
</select></p>

<p><strong>I will attend:</strong><br />
<input type="radio" name="day" value="Wednesday"></input>Wednesday only<br />
<input type="radio" name="day" value="Thursday"></input>Thursday only<br />
<input type="radio" checked="checked" name="day" value="Both"></input>Wednesday and Thursday</p>

<p><strong>Lodging</strong><br />
<input type="radio" name="lodging" value="Elephant Butte Inn" checked="checked" ></input>
I will lodge overnight at Elephant Butte Inn<br />
<input type="radio" name="lodging" value="Own Arrangements" ></input>I will make my own lodging arrangements.</p>

<p><strong>I will have dinner Wednesday night at Elephant Butte Inn:</strong><br />
<input type="radio" name="dinner" value="Yes" checked="checked" ></input>Yes<br />
<input type="radio" name="dinner" value="No" ></input>No</p>

<p><strong>Please indicate any special food requirements</strong><br />
(vegetarian, wheat or dairy allergies, etc.):<br />
<textarea name="special" rows="5" cols="60"></textarea></p>

<p><input type="submit" value="Register"></input>
<input type="reset" value="Reset"></input></p>
</form>
--------------------------------------------------------------------------------------

Here is the Pearl script:
--------------------------------------------------------------------------------------
$mailprog = '/usr/sbin/sendmail';

# ==================================================================================================
# Email address of the ADVANCE department being notified of registration (paid@advance.nmsu.edu)
# ==================================================================================================

$recipient = 'rleseth@nmsu.edu';

# ==================================================================================================
# Print out a content-type for HTTP/1.0 compatibility
# ==================================================================================================

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

# ==================================================================================================
# Get the input from the submitted form
# ==================================================================================================

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

# ==================================================================================================
# Split the name-value pairs
# ==================================================================================================

@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);

# ==================================================================================================
# Un-Webify plus signs and %-encoding
# ==================================================================================================

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$Value =~ s/<!--( . | \n) *-->//g;

# Stop people from using subshells to execute commands
# Not a big deal when using sendmail, but very important
# when using UCB mail (aka mailx).
# $value =~ s/~!/ ~!/g;

# Uncomment for debugging purposes
# print "Setting $name to $value<P>";

$FORM{$name} = $value;
}
print MAIL "---------------------------------------------------------\n";
print MAIL "*************** Dept Head Retreat Registration ****************\n";
print MAIL "---------------------------------------------------------\n\n";

# =================================================================================================
# Demographic data on registrant emailed to ADVANCE (paid@nmsu.edu)
# =================================================================================================

print MAIL "---------------------------------------------------------\n";
print MAIL "Name: $FORM{'fullname'}\n";
print MAIL "Title: $FORM{'title'}\n";
print MAIL "Email: $FORM{'email'}\n";
print MAIL "Phone: $FORM{'phone'}\n";
print MAIL "Affiliation: $FORM{'affiliation'}\n";
print MAIL "Lodging: $FORM{'day'}\n";
print MAIL "Dinner: $FORM{'dinner'}\n";
print MAIL "Special Food: $FORM{'special'}\n";

print MAIL "---------------------------------------------------------\n\n";
close (MAIL);

# =================================================================================================
# Send confirmation of events selected to registrant via their web page
# =================================================================================================

# =================================================================================================
# Print title and initial heading to registrants web browser
# =================================================================================================

print "<Body background=\"/images/background.gif\"><H2>ADVANCE Event Registration Confirmation</H2><hr><p>";
print "<B>Thank you</B> for your registration.<BR><BR>";
print "<B>---------------------------------------------------------------------------------------</B><BR>";

# =================================================================================================
# Print all selected events to registrants web browser
# =================================================================================================

print "<B>---------------------------------------------------------------------------------------</B><BR>";

# =================================================================================================
# Print registrants demographic data to their web browser
# =================================================================================================

print "Name: $FORM{'fullname'}<BR>";
print "Title: $FORM{'title'}<BR>";
print "Email Address: $FORM{'email'}<BR>";
print "Daytime Telephone: $FORM{'phone'}<BR>";
print "Affiliation: $FORM{'affiliation'}<BR>";
print "I Will Attend: $FORM{'day'}<BR>";
print "Lodging: $FORM{'lodging'}<BR>";
print "Dinner: $FORM{'dinner'}<BR>";
print "Special Food Requirements: $FORM{'special'}<P><P>";
print "<B>---------------------------------------------------------------------------------------</B><BR>";

# =================================================================================================
# Print request asking registrant to print their confirmation page and provide link back to event page
# =================================================================================================

print "Please <B>print a copy</B> of this confirmation for your records.<BR>";

print "<B>---------------------------------------------------------------------------------------</B><BR>";

print "<BR>Return to the <A HREF=\"http://www.advance.nmsu.edu\">ADVANCE Home Page</A>.<P>";

# =================================================================================================
# Subroutine (blank_response) to notify registrant that they have excluded required fields
# =================================================================================================

sub blank_response
{
print "<H2><font color=red>Error!</font> - A required field was left blank!</H2><P>";
print "Please enter information for all <B>required</B> fields on the registration form.<P>";
print "Call xxxx at xxx-xxxx if you need assistance.<P>";
print "Thank You,<P>";
print "Return to the <A HREF=\"http://www.advance.nmsu.edu/PAID/Documents/Retreat08_RegForm.html\">Registration Form</A>.<P>";

exit;
}
-------------------------------------------------------------------------------------

Any assistance will be greatly appreciated............Ray

FishMonger
12-10-2007, 07:28 PM
To start with, the enctype should be:
enctype="multipart/form-data"

The CGI module is the de-facto standard for cgi processing. The CGI module has 2 interface styles, functional and OO (object orientated). I prefer the OO approach. Also, the warnings and strict pragmas should be used in every perl script you write

use warnings;
use strict;
use CGI;

my $cgi = CGI->new;

print $cgi->header;

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

# ==================================================================================================
# Split the name-value pairs
# ==================================================================================================

@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);

# ==================================================================================================
# Un-Webify plus signs and %-encoding
# ==================================================================================================

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$Value =~ s/<!--( . | \n) *-->//g;

# Stop people from using subshells to execute commands
# Not a big deal when using sendmail, but very important
# when using UCB mail (aka mailx).
# $value =~ s/~!/ ~!/g;

# Uncomment for debugging purposes
# print "Setting $name to $value<P>";

$FORM{$name} = $value;
}That approach to form processing has been depreciated for more than 10 years.

Here's the replacement for your form processing.
my %form = $cgi->Vars;

You must have left out portions of your script, because you're sending output the the MAIL filehandle which isn't defined in your posted code. Personally, I'd use one of Perl's mail modules, such as MIME::Lite (http://search.cpan.org/author/RJBS/MIME-Lite-3.021/lib/MIME/Lite.pm), rather than manually opening a pipe to sendmail.

roadrunnerray
12-10-2007, 07:51 PM
Hi FishMonger, many thanks for your reply. I need a little time to digest your reply and will return to let you know the results. Looks like I need to move into the 21st century with regard to Pearl. And yes, I left out portions of the script.............Ray

FishMonger
12-10-2007, 07:59 PM
print "Name: $FORM{'fullname'}<BR>";
print "Title: $FORM{'title'}<BR>";
print "Email Address: $FORM{'email'}<BR>";
print "Daytime Telephone: $FORM{'phone'}<BR>";
print "Affiliation: $FORM{'affiliation'}<BR>";
print "I Will Attend: $FORM{'day'}<BR>";
print "Lodging: $FORM{'lodging'}<BR>";
print "Dinner: $FORM{'dinner'}<BR>";
print "Special Food Requirements: $FORM{'special'}<P><P>";
print "<B>---------------------------------------------------------------------------------------</B><BR>";
The print() function is a list operator, so rather than using multiple print statements, it would be better and cleaner to pass it a list or use a here document.

List:print $cgi->br("Name: $FORM{'fullname'}"),
$cgi->br("Title: $FORM{'title'}"),
$cgi->br("Email Address: $FORM{'email'}"),
$cgi->br("Daytime Telephone: $FORM{'phone'}"),
$cgi->br("Affiliation: $FORM{'affiliation'}"),
$cgi->br("I Will Attend: $FORM{'day'}"),
$cgi->br("Lodging: $FORM{'lodging'}"),
$cgi->br("Dinner: $FORM{'dinner'}"),
$cgi->br("Special Food Requirements: $FORM{'special'}<P><P>");

print $cgi->b('-' x 80);

Here Document:
print <<"FORM_SUBMISSION";
Name: $FORM{'fullname'}<BR>
Title: $FORM{'title'}<BR>
Email Address: $FORM{'email'}<BR>
Daytime Telephone: $FORM{'phone'}<BR>
Affiliation: $FORM{'affiliation'}<BR>
I Will Attend: $FORM{'day'}<BR>
Lodging: $FORM{'lodging'}<BR>
Dinner: $FORM{'dinner'}<BR>
Special Food Requirements: $FORM{'special'}<P><P>
<B>---------------------------------------------------------------------------------------</B><BR>
FORM_SUBMISSION

roadrunnerray
12-10-2007, 08:32 PM
Thanks again FishMonger, I am working with your suggestions. I thought I would list the entire script that I modified a few years back in case there was a problem in the portion that I did not list. It was created in 1994!

I replaced the enctype in the form definition and tried the "List" and "Here Document." The "Here Document: format printed the field descriptors but not the data. the "List" format did not print the descriptors or data.

I am now going to make the other changes you suggested. where do I place the:

use warnings;
use strict;
use CGI;

my $cgi = CGI->new;

print $cgi->header;

in the script?

---------------------------------------------------------------------------------------
#!/usr/bin/perl -- -*-perl-*-

# ------------------------------------------------------------
# Form-mail.pl, by Reuven M. Lerner (reuven@the-tech.mit.edu).
#
# Last updated: March 14, 1994
#
# Form-mail provides a mechanism by which users of a World-
# Wide Web browser may submit comments to the webmasters
# (or anyone else) at a site. It should be compatible with
# any CGI-compatible HTTP server.
#
# Please read the README file that came with this distribution
# for further details.
# ------------------------------------------------------------

# ------------------------------------------------------------
# This package is Copyright 1994 by The Tech.

# Form-mail is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.

# Form-mail is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Form-mail; see the file COPYING. If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# ------------------------------------------------------------

# Define fairly-constants

# ==================================================================================================
# Define and locate email program to be used for notifying ADVANCE
# ==================================================================================================

$mailprog = '/usr/sbin/sendmail';

# ==================================================================================================
# Email address of the ADVANCE department being notified of registration (paid@advance.nmsu.edu)
# ==================================================================================================

$recipient = 'rleseth@nmsu.edu';

# ==================================================================================================
# Print out a content-type for HTTP/1.0 compatibility
# ==================================================================================================

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

# ==================================================================================================
# Get the input from the submitted form
# ==================================================================================================

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

# ==================================================================================================
# Split the name-value pairs
# ==================================================================================================

@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);

# ==================================================================================================
# Un-Webify plus signs and %-encoding
# ==================================================================================================

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$Value =~ s/<!--( . | \n) *-->//g;

# Stop people from using subshells to execute commands
# Not a big deal when using sendmail, but very important
# when using UCB mail (aka mailx).
# $value =~ s/~!/ ~!/g;

# Uncomment for debugging purposes
print "Setting $name to $value<P>";

$FORM{$name} = $value;
}

# =================================================================================================
# Upload to (ftp://admin@128.123.83.183/WebServer/CGI-Executables/ADVANCERegForm.pl)
# =================================================================================================

# =================================================================================================
# Check for blank data fields submmitted by registrant and notify them to complete all required fields
# =================================================================================================

#&blank_response unless $FORM{'textfieldfullname'};
#&blank_response unless $FORM{'textfieldtitle'};
#&blank_response unless $FORM{'textfieldemail'};
#&blank_response unless $FORM{'textfieldphone'};

# ==================================================================================================
# Notify ADVANCE via email to $recipient (paid@ADVANCE.nmsu.edu) of new registration
# ==================================================================================================

open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";

# =================================================================================================
# Demographic data on registrant emailed to ADVANCE (paid@nmsu.edu)
# =================================================================================================

print MAIL "---------------------------------------------------------\n";
print MAIL "*************** Dept Head Retreat Registration ****************\n";
print MAIL "---------------------------------------------------------\n\n";

# =================================================================================================
# Demographic data on registrant emailed to ADVANCE (paid@nmsu.edu)
# =================================================================================================

print MAIL "---------------------------------------------------------\n";
print MAIL "Name: $FORM{'fullname'}\n";
print MAIL "Title: $FORM{'title'}\n";
print MAIL "Email: $FORM{'email'}\n";
print MAIL "Phone: $FORM{'phone'}\n";
print MAIL "Affiliation: $FORM{'affiliation'}\n";
print MAIL "Lodging: $FORM{'day'}\n";
print MAIL "Dinner: $FORM{'dinner'}\n";
print MAIL "Special Food: $FORM{'special'}\n";

print MAIL "---------------------------------------------------------\n\n";
close (MAIL);

# =================================================================================================
# Send confirmation of events selected to registrant via their web page
# =================================================================================================

# =================================================================================================
# Print title and initial heading to registrants web browser
# =================================================================================================

print "<Body background=\"/images/background.gif\"><H2>ADVANCE Event Registration Confirmation</H2><hr><p>";
print "<B>Thank you</B> for your registration.<BR><BR>";
print "<B>---------------------------------------------------------------------------------------</B><BR>";

# =================================================================================================
# Print registrants demographic data to their web browser
# =================================================================================================

print <<"FORM_SUBMISSION";
Name: $FORM{'fullname'}<BR>
Title: $FORM{'title'}<BR>
Email Address: $FORM{'email'}<BR>
Daytime Telephone: $FORM{'phone'}<BR>
Affiliation: $FORM{'affiliation'}<BR>
I Will Attend: $FORM{'day'}<BR>
Lodging: $FORM{'lodging'}<BR>
Dinner: $FORM{'dinner'}<BR>
Special Food Requirements: $FORM{'special'}<P><P>
<B>---------------------------------------------------------------------------------------<BR>
FORM_SUBMISSION

# =================================================================================================
# Print request asking registrant to print their confirmation page and provide link back to event page
# =================================================================================================

print "Please <B>print a copy</B> of this confirmation for your records.<BR>";

print "<B>---------------------------------------------------------------------------------------</B><BR>";

print "<BR>Return to the <A HREF=\"http://www.advance.nmsu.edu\">ADVANCE Home Page</A>.<P>";

# =================================================================================================
# Subroutine (blank_response) to notify registrant that they have excluded required fields
# =================================================================================================

sub blank_response
{
print "<H2><font color=red>Error!</font> - A required field was left blank!</H2><P>";
print "Please enter information for all <B>required</B> fields on the registration form.<P>";
print "Call xxxx at xxx-xxxx if you need assistance.<P>";
print "Thank You,<P>";
print "Return to the <A HREF=\"http://www.advance.nmsu.edu/PAID/Documents/Retreat08_RegForm.html\">Registration Form</A>.<P>";

exit;
}
------------------------------------------------------------------------------------

Many thanks for your kind assistance...............Ray

FishMonger
12-10-2007, 08:45 PM
The loading of the modules should be at the top of the script, just under the shebang line.

That script is really old and should be updated or replaced.

If you want to go the replacement route, take a look at either the FormMail or TFMail scripts from the nms project.
http://nms-cgi.sourceforge.net/scripts.shtml

roadrunnerray
12-10-2007, 08:51 PM
FishMonger, thanks again, I will look at the scripts in the link you gave me and create a new script.........................Ray

roadrunnerray
12-17-2007, 11:04 PM
The loading of the modules should be at the top of the script, just under the shebang line.

That script is really old and should be updated or replaced.

If you want to go the replacement route, take a look at either the FormMail or TFMail scripts from the nms project.
http://nms-cgi.sourceforge.net/scripts.shtml

Hi FishMonger, wanted to let you know I went to the NMS Project page above and used the FormMail-compat script they provided. It worked like a "charm" and is quite flexible. Thanks again for the guidance............Ray