CGI Script to accept data from a form

I've written an HTML form and I'd like to send the info to a cgi script storing the info to a html form and sending back results to the user...

here is my html form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE> Service Registration</TITLE>
</HEAD>
<BODY background="./tecbkgnd.gif" bgcolor="#FFFFFF" text="#000000" link="#663300&quotvlink="#006666" alink="#FFCC00">

<!--ENDOFADD -->
<FORM ACTION="http://atslink.com/cgi-bin/service.pl" METHOD="GET" ENCTYPE="application/x-www-form-urlencoded">
<P><INPUT TYPE="HIDDEN" NAME="FormId" SIZE="-1" VALUE="FormResult"></P>
<P><INPUT TYPE="HIDDEN" NAME="UserNbr" SIZE="-1" VALUE="1"></P>
<P><INPUT TYPE="HIDDEN" NAME="FormNbr" SIZE="-1" VALUE="5"></P>
<P><INPUT TYPE="HIDDEN" NAME="FormKey" SIZE="-1" VALUE="07721161"></P>
<CENTER><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 WIDTH="600"><TR><TD>
<A NAME="ItemAnchor0"></A>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 WIDTH=100%><TR><TD>
<CENTER><FONT SIZE="5" ><B>Service Registration</B></FONT></CENTER></TD>
<A NAME="ItemAnchor1"></A>
</TR></TABLE><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 WIDTH=100%><TR><TD>
<CENTER><FONT SIZE="" ><BR><TABLE BORDER="0" WIDTH="80%" CELLPADDING="0" CELLSPACING="0">
<TR><TD BGCOLOR="#000099"><FONT COLOR="#FFFFFF">
This is a sample Registration form.  Note the
hand next to each of the items.  This indicates
that a response is required for this question.  
</FONT></TD></TR></TABLE><BR>

</FONT></CENTER></TD>
<A NAME="ItemAnchor2"></A>
</TR></TABLE><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT><IMG SRC="./r_hand.gif" ALT="response required">
<B>First Name</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-2" SIZE="15" MAXLENGTH="25">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor3"></A>
<TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT><IMG SRC="./r_hand.gif" ALT="response required">
<B>Last Name</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-3" SIZE="18" MAXLENGTH="25">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor4"></A>
</TR></TABLE><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT><IMG SRC="./r_hand.gif" ALT="response required">
<B>Address 1</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-4" SIZE="35" MAXLENGTH="35">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor5"></A>
</TR></TABLE><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT>
<B>Address 2</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-5" SIZE="35" MAXLENGTH="35">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor6"></A>
</TR></TABLE><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT><IMG SRC="./r_hand.gif" ALT="response required">
<B>City</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-6" SIZE="15" MAXLENGTH="35">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor7"></A>
<TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT><IMG SRC="./r_hand.gif" ALT="response required">
<B>State</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-7" SIZE="2" MAXLENGTH="2">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor8"></A>
<TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT><IMG SRC="./r_hand.gif" ALT="response required">
<B>Zip</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-8" SIZE="10" MAXLENGTH="15">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor9"></A>
</TR></TABLE><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR><TD>
<FONT><IMG SRC="./r_hand.gif" ALT="response required">
<B>Phone</B>
</FONT></TD>
</TR><TR>
<TD>
<FONT><INPUT TYPE="TEXT" NAME="RESULT_TextField-9" SIZE="25" MAXLENGTH="25">
</FONT></TD></TR></TABLE>
</TD>
<A NAME="ItemAnchor10"></A>
</TR></TABLE><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 WIDTH=100%><TR><TD>
<FONT SIZE="" ><BR></FONT></TD>
<A NAME="ItemAnchor11"></A>
</TD></TR><TR><TD><IMG SRC="./r_hand.gif" ALT="response required">indicates response required</CENTER></TD></TR></TABLE>
</TD></TR></TABLE></CENTER>
<P><CENTER><INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit"><INPUT TYPE=reset VALUE="Reset"></CENTER>
</FORM>
</BODY>
</HTML>


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

Here is what I've come up with for the CGI so far, but I've never done this and I'm a bit stumped

#!/usr/bin/perl -w

  open (DATA, "/www/service/formdata.html");  ###I would like to append to the file so Im not creating a new file everytime.

  my @data = <DATA>;

  close (DATA);


my $in;

  if ($ENV{'REQUEST_METHOD'} eq "GET") {

       $in = $ENV{'QUERY_STRING'};

  } else {

       $in = <STDIN>;

  }

$in =~ s/%(..)/pack("c",hex($1))/ge;    If im not mistaken this will read the info in and format it so I can save it

urobinsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
Use the standard Perl CGI module for doing CGI work, eg:

#!/usr/bin/perl
use strict;
use warnings;
#use CGI::Carp qw(fatalsToBrowser);
use CGI;

my $q = new CGI;
my %p = $q->Vars;
my $formdata = '/www/service/formdata.html';

open DATA, ">>$formdata" or die "Can not append to $formdata $!\n";

print $q->header;
print $q->h1('Thank you');

print DATA <<EOF;
print Field1: RESULT_TextField1
Field2: RESULT_TextField2
etc
EOF


or use a pre-written one like FormMail from http://nms-cgi.sourceforge.net/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
urobinsAuthor Commented:
How would I go about checking to make sure there was data inputed so Im not putting blank data in the formdata.html?
urobinsAuthor Commented:
Sorry to be a hassle but Im also a little confused about this

print DATA <<EOF;
print Field1: RESULT_TextField1
Field2: RESULT_TextField2

EOF

is that all I have to do to print those values is

Field2:RESULT_TextField2
Field3:RESULT_TextField3

and step through?
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

TintinCommented:
My mistake with the code.

print DATA <<EOF;
print Field1: RESULT_TextField1
Field2: RESULT_TextField2

EOF

should be:

print DATA <<EOF;
print Field1: $p{'RESULT_TextField1'}
Field2: $p{'RESULT_TextField2'}

EOF

The line

my %p = $q->Vars;

creates a hash of all your CGI name/value pairs.  You could also loop through this list to print out all the values.

For validating code, you can do things like:

die "Field must not be blank\n" unless $p{'RESULT_TextField1'};
die "Field must contain at least one digit\n" unless ($p{'RESULT_TextField1'} =~ /\d/);
urobinsAuthor Commented:
Okay I'll give that a shot, I appreciate your help

so the die woud go in my CGI code when I am writing it to the file?  

Sorry really new to this stuff and doing my best to get a handle on it.
TintinCommented:
You will *always* want to validate your data before you do anything like open a file or display anything to the user, so put the checks near the top, eg:

#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI;

my $q = new CGI;
my %p = $q->Vars;

die "Field must not be blank\n" unless $p{'RESULT_TextField1'};
die "Field must contain at least one digit\n" unless ($p{'RESULT_TextField1'} =~ /\d/);
etc
urobinsAuthor Commented:
Okay thanks.  Im gonna give that a shot.  Im going to award you the points for all of the assistance, but would you mind checking back here just in case I run into another problem ?  Thanks again you have been very helpful!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.