?
Solved

CGI Script to accept data from a form

Posted on 2006-04-23
7
Medium Priority
?
213 Views
Last Modified: 2010-03-05
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

0
Comment
Question by:urobins
  • 4
  • 3
7 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 16521312
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/

0
 

Author Comment

by:urobins
ID: 16521376
How would I go about checking to make sure there was data inputed so Im not putting blank data in the formdata.html?
0
 

Author Comment

by:urobins
ID: 16521426
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Tintin
ID: 16521582
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/);
0
 

Author Comment

by:urobins
ID: 16521655
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.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 16522212
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
0
 

Author Comment

by:urobins
ID: 16523362
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!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question