Google Answers Logo
View Question
Q: HTML Form Processing ( Answered 5 out of 5 stars,   0 Comments )
Subject: HTML Form Processing
Category: Computers > Internet
Asked by: vlad-ga
List Price: $10.00
Posted: 16 Feb 2003 17:20 PST
Expires: 18 Mar 2003 17:20 PST
Question ID: 162252

I have a form on my website that is for market research purposes. 
Rather than email the results of each and every filled out form to me,
I would like that the form results are posted on a web page in a
manner where I can copy and then collate them into an Excel
spreadsheet.  (Since, I don't want to deal with a DB right now, I
thought of perhaps modifying a 'guestbook'-type perl
script to do this.)

So, I am looking for a a quick and easy way to do this.  Hopefully,
with as little perl as possible.  Any script you provide, please
explain thoroughly.  Thanks.


Request for Question Clarification by snapanswer-ga on 16 Feb 2003 20:29 PST
Can you tell us which form mail script you are using.  I may help us
to provide the easiest answer for you to implement.

(If you did not choose your form mail script, it may be that we can
check with your ISP or at times, the "action" portion of your <form>

Subject: Re: HTML Form Processing
Answered By: joey-ga on 16 Feb 2003 20:55 PST
Rated:5 out of 5 stars
Hi there.  For this answer, I'm going to assume that you're somewhat
aware of how PERL works.

The following script is a complete working script that will accept
input from a form, decode it, split it into appropriate variables, and
output it to a tab-delimited text file.

You can remove the entire decoding section if you use a PERL include
to do this . . . just make sure that you still strip out the

Once you've implemented this file, you'll be able to type in the
address to the saved-data text file.  You can select and copy the
contents from your browser and then paste-special them into excel as
tab-delimited values.

Please let me know if you have any questions.

As a note, this script doesn't check for people using a TAB in their
form submission.  Browsers generally don't allow you to press TAB in a
textarea or text field box, so this shouldn't be a problem.  But, if
someone has copied and pasted a tab from another program into the text
box, it could misalign that row.  You can strip out tabs just like
I've stripped out line breaks below.




#### call the CGI subroutines from below

#### location of output file.  Leave out the directory
#### structure if you want the file to show up in this directory
open (TSVOUT, ">> /path/to/csv/file.txt");

#### cycle through all the form fields sent to this script
foreach $field (@fieldlist) {
    #### make sure a TAB character is shown after the variable
    $line .= "$FORM{$field}	";

#### output the tab-delimited fields to the TSV file
print TSVOUT "$line\n";

close (TSVOUT);


#### these are basic CGI decoding routines
#### you can swap these out for PERL includes if you like

sub cgi_receive {
    if ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
    } else {
        $incoming = $ENV{'QUERY_STRING'};

sub cgi_decode {
    @pairs = split(/&/, $incoming);

    @fieldlist = ( );

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

        #### Convert line breaks to spaces
        $value =~ s/%0D%0A/  /g;
        $name  =~ tr/+/ /;
        $value =~ tr/+/ /;
        $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
        $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

        #### Convert tabs to spaces
        #### make sure a TAB character is between the 1st two slashes
        $value =~ s/	/     /g;

        #### Strip out exclamation points except in sentences
        $value =~ s/\|/ /g;
        $value =~ s/^!/ /g;

        #### Allow for multiple values of a single name
        if ($FORM{$name}) {
             $FORM{$name} .= ", ";
        } else {
             @fieldlist = (@fieldlist, $name);
        $FORM{$name} .= $value;

Clarification of Answer by joey-ga on 16 Feb 2003 20:58 PST
Hey, Google wrapped the script in one place and this will cause an
error unless you "unwrap" it . . .

Take the line that shows the characters "print-out" (line 15) and
place it at the end of the previous line (line 14) with the comment.

Let me know if you have any problems.

Clarification of Answer by joey-ga on 16 Feb 2003 20:59 PST
Oh, lastly, I forgot that I'd actually fixed the "TAB" bug mentioned
in the initial error before I even posted this.  The script I posted
reflects the change, so you should not have to worry about someone's
entering of a TAB character misaligning the file.

Request for Answer Clarification by vlad-ga on 16 Feb 2003 23:42 PST
Hi Joey,

I think that is what I want.  A couple quick questions:

1)  Just to make sure - after the first form is filled out and posted
in the text file, the subsequent forms do not overwrite the first,
right?  I assume they are appended one after another.

2)  Regarding the script, could you walk me through 'installing' the
script?  I don't imagine it just as easy as renaming the script



Clarification of Answer by joey-ga on 17 Feb 2003 09:16 PST
1. That's correct . . . it will "append" the output to the end.  The
">>" character in the output line signifies this.  Had it merely been
a single ">", it would have overwritten the old stuff.

2. Just change the script to be called whatever you like (with a .pl
or .cgi extention based on what your web server is set to use . . .
it's going to depend on your web server's configuration how to be sure
of this.)  Then, make sure the directory the script is in has CGI
permissions one way or another (again, this will be determined by your
web server's configuration.)

Then, you must make the file readable and executable by the web
server.  If this is UNIX (or Linux, etc.) you can do this with the
following command:

     chmod a+rx my_cgiscript_filename.cgi

Then, you must create a blank file called "file.txt" (or whatever
you've chosen to call it) in the appropriate directory.  This can be
done using any basic text editor and saving the file.  Then, you'll
have to make this file readable and writeable to the web server:

     chmod a+rw file.txt

Good luck.


Request for Answer Clarification by vlad-ga on 20 Feb 2003 00:32 PST
Hi Joey,

I don't mean to be a pest but I am having a spot of trouble with this:

Couple questions: 

1) Regarding Google wrapping the script - It should look this, right?

(LINE-X)foreach $field (@fieldlist) { 
(LINE-Y)#### make sure a TAB character is shown after the variable
(LINE-Z)$line .= "$FORM{$field} "; 
2)  Where do I place the text file?  In cgi-bin?  In public_html? 

3)  How can I tell (not having root access) what to name the file,
that is, .pl or .cgi?



Clarification of Answer by joey-ga on 20 Feb 2003 06:44 PST
1. You got it.

2. You can place the text file anywhere you like in the WWW root. 
Note, if you put the text file in any location other than in the
location of the CGI script you need to type in the ENTIRE system
directory.  This is NOT merely the directory structure seen when you
type in a URL.  If you choose to put it in the "public_html"
directory, the directory structure you would type in would be
something like "/home/username/public_html/file.txt" or something to
that effect.  It depends on your server's configuration.

3. You'll need to consult your system administrators regarding naming
the file.  My suggestion is to try it both ways to see if one way
works and the other doesn't.

Take care.

Request for Answer Clarification by vlad-ga on 21 Feb 2003 19:48 PST
Hi Joey,

I am at my wit's end.  I keep getting an error page and nothing shows
up in file.txt.

This the html I am using to test it:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
<table cellspacing="5" cellpadding="0" width="600" align="center"
bgcolor="#ffffff" border="0">
<td valign="top" align="left"></td>

<td valign="center" align="right">
<form action="/cgi-bin/vlad.cgi" method="post">
<table cellspacing="0" cellpadding="2" width="600" border="0">
<tr valign="center" align="left" bgcolor="#CC9900">
<td colspan="2" height="16"><b><font face=
"Verdana, Arial, Helvetica, sans-serif" color="#000000" size=
"1">Contact Information</font></b></td>

<td valign="center" align="right" width="150"><font face=
"Verdana, Arial, Helvetica, sans-serif" size="1">First
<td valign="center" align="left" width="450"><font face=
"Verdana, Arial, Helvetica, sans-serif" size="1"><input size="30"

<td bgcolor="#CC9900" colspan="2" height="16"></td>

<td width="150"> </td>
<td valign="bottom" align="left" width="450"><font face=
"Verdana, Arial, Helvetica, sans-serif" size="1"><input type=
"submit" value="Submit" name="Submit"> <input type="reset" value=
"Clear" name="Submit2"></font></td>

I also set the permissions as you instructed - but it not


Request for Answer Clarification by vlad-ga on 21 Feb 2003 19:51 PST
BTW This is the error page I am getting:

Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator and inform them of the time
the error occurred, and anything you might have done that may have
caused the error.

More information about this error may be available in the server error

Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.


Clarification of Answer by joey-ga on 21 Feb 2003 23:02 PST
Hi.  You're getting an internal server error, maybe, because you
aren't feeding any text back to the browser.  So, the data should
still be stored in the file, but the error will appear b/c you're not
printing anything . . .

To have something print to the screen, add these lines immediately
before the subroutines at the bottom:

print "Content-type: text/html\n";
print "Status: 302 Redirected\n";
print "Location:\n\n";

That will then redirect the browser upon submission to a page of your

Once you've done that, see if the error still shows up.
vlad-ga rated this answer:5 out of 5 stars

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy