Gossamer Forum
Home : General : Perl Programming :

FormMail mod that writes to database?

Quote Reply
FormMail mod that writes to database?
I'm trying to create a database of student scores to manage with dbman.

Currently, I use a quiz-making program, Hot Potatoes, that POSTS student scores (form data) to an email that I receive (as teacher), using FormMail.pl.

What is posted is simple stuff--name, score, start and finish time--in a standard form that FormMail understands.

Is there a modification of FormMail that gives the option of posting that data also to a text file database?

I've tried bnbform and yform, but they do not accept data in the same form that FormMail requires. I can make them work, but they require me to modify the Hot Potatoes code, which is awkward and causes new problems, so I'm hoping to find another CGI form processor.

Thanks,

Gerald
Quote Reply
Re: [ggrow] FormMail mod that writes to database? In reply to
Hi Gerald,

At the end of the 'send_mail' routine add this subroutine call

&log_add;

Then somewhere in your script add the subroutine

Code:
sub log_add
{

open (TXTFILE, ">>path/to/file") or die "Cant open path to file $!";
print TXTFILE "-- Latest Entry --\n";
foreach $field (@Field_Order) {
if ($Config{'print_blank_fields'} || $Form{$field}) {
print TXTFILE "$field: $Form{$field}\n";
}
}
print TXTFILE "--------------------------------------------";
print TXTFILE "\n";
close(TXTFILE);

}
Of course you can play with it to suit, but you should get the idea

Brad
Quote Reply
Re: [six] FormMail mod that writes to database? In reply to
Brad,

Thanks for the code! I hate to ask for more, but I am at best a cut-and-paste coder, and tinkering takes me far more than it would take anyone who knew anything.

Do you know a way to get FormMail to save each set of POSTed fields to a text database so that each record comes in a single line, with fields delimited by something convenient like | and a return at the end?

I'm trying to get things into a form that I can readily download into Excel.

Thanks again,

Gerald
Quote Reply
Re: [ggrow] FormMail mod that writes to database? In reply to
Brad,

Thanks a million.
Your mod worked, and I fiddled with it and managed to make it print the output line by line, as I needed.

I feel you have dragged me kicking and screaming into perl programming.

Gerald
Quote Reply
Re: [ggrow] FormMail mod that writes to database? In reply to
Can you post the changes you made :)

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] FormMail mod that writes to database? In reply to
Sure.

I altered the standard, familiar, beloved FormMail.pl cgi script so that it also saves to a text file, without changing anything else. There are several mods of FormMail that save to a text file (e.g., bnbform, yform), but all of them change other aspects of FormMail so that you cannot simply shift to them without changing your forms scripts radically.

This mod does not alter the way FormMail works. It just adds a function.

The full version of the resulting file, formmailfile.pl, with documentation, can be downloaded at:

http://www.longleaf.net/formmailfile/

The changes are fully marked off and commented.

Basically I added:

(1) the name of a new subroutine, &log_add
(2) a new variable where you assign the name of the file to write the data to, $datafilename='textfile.db'; (here defining the output file as textfile.db).
(3) the subroutine itself, which is:

sub log_add
{

# The following line contains the name of the file where the variables are
# saved, $datafilename, which was defined in the beginning of the code.
# The datafile delimiter is set in two places, here as a |, entered below
# with a leading backslash, so it will not be read as a control character.

open (TXTFILE, ">>$datafilename") or die "Cant open path to file $!";
print TXTFILE "$Config{'realname'}\|";
foreach $field (@Field_Order) {
if ($Config{'print_blank_fields'} || $Form{$field}) {
print TXTFILE "$Form{$field}\|";
}
}
print TXTFILE "\n";
close(TXTFILE);

}


For the forms I am POSTING, the output is a data file containing the following on each successive line:

name | score | exercise | start time | endtime |

This is exactly the same info, in the same order, that FormMail emails to "recipient." With the info in a datafile, I find it much easier to download it and work with it in Excel, than to read a dozen or a hundred emails with these scores.

I am not sure how secure any of this is, and I would hesitate to trust valuable data to it. But for my purposes -- collecting student scores and downloading them shortly after -- it is effective.

If any experienced programmer has comments or suggestions, I would be grateful to hear them. This is strictly a trial-and-error hack made out of necessity by someone who has not programmed anything since Microsoft Basic version 1 on CP/M in 1981.

One question I have is this: Is there any way to assign $datafilename a full path, http://www.server.com/... etc., or is it limited only to local paths? I have not been able to get the full path to work.

Thanks to all,

Gerald
Quote Reply
Re: [ggrow] FormMail mod that writes to database? In reply to
Thank you for providing your changes :)

You can assign it using the full PATH not a URL.
So the path would look something like:

/usr/local/etc/httpd/ .....
or
/usr/home1/lauction/html/db....

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [ggrow] FormMail mod that writes to database? In reply to
Hi Gerald,

Glad it helped...went on an 5 day mini vacation with some family and friends completely away from the 'puter, never happened before...

Like LoisC wrote, whatever your path structure is to the script, such as

/home/user/public_html/cgi-bin/formmail.pl

can also be used to construct the full path to the file

/home/user/public_html/results/textfile.db

formmail, or any script for that matter(as far as I know) will not write to a file using a URL, only a path...

Brad