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
line-breaks.
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.
--Joey
------------------------------------------------------
#!/usr/bin/perl
#### call the CGI subroutines from below
&cgi_receive;
&cgi_decode;
#### 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
print-out
$line .= "$FORM{$field} ";
}
#### output the tab-delimited fields to the TSV file
print TSVOUT "$line\n";
close (TSVOUT);
exit;
#### SUBROUTINES ####
#### 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
foo.pl???
Thanks
vlad
|
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.
--Joey
|
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
print-out
(LINE-Z)$line .= "$FORM{$field} ";
(LINE-Z1)}
2) Where do I place the text file? In cgi-bin? In public_html?
Elsewhere?
3) How can I tell (not having root access) what to name the file,
that is, .pl or .cgi?
Thanks,
vlad
|
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">
<html>
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
<title></title>
</head>
<body>
<table cellspacing="5" cellpadding="0" width="600" align="center"
bgcolor="#ffffff" border="0">
<tbody>
<tr>
<td valign="top" align="left"></td>
</tr>
<tr>
<td valign="center" align="right">
<form action="/cgi-bin/vlad.cgi" method="post">
<table cellspacing="0" cellpadding="2" width="600" border="0">
<tbody>
<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>
</tr>
<tr>
<td valign="center" align="right" width="150"><font face=
"Verdana, Arial, Helvetica, sans-serif" size="1">First
Name</font></td>
<td valign="center" align="left" width="450"><font face=
"Verdana, Arial, Helvetica, sans-serif" size="1"><input size="30"
name="firstname"></font></td>
</tr>
<tr>
<td bgcolor="#CC9900" colspan="2" height="16"></td>
</tr>
<tr>
<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>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
------------------------------------------------------
I also set the permissions as you instructed - but it not
working!!!!?????
vlad
|
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
log.
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: http://www.wherever.com/page.html\n\n";
That will then redirect the browser upon submission to a page of your
choosing.
Once you've done that, see if the error still shows up.
|