Gossamer Forum
Home : General : Perl Programming :

form script modification

Quote Reply
form script modification
I receive e-mails from people looking for more information about helping the environment. So I created a form letter. To save myself some time - I created a webpage with a form. From their e-mail, I cut and paste their info into the form and hit submit. The script sends them a message with the info they were asking about, and sends me a different message with their vital info - name and email.

I would like to prevent duplication - should I receive multiple requests from the same person. To do this, I would like to know how to add a line (or lines) to the script - see attached - that would record their e-mail address to flat file database. Then, when I submit - it checks the addresses in the database for duplication. If the address is new - it sends, if not - it takes you to a page that says information has already been sent to this address.
Quote Reply
Re: [shiner] form script modification In reply to
To match the address:

Code:
my $found = 0;

open (EML, "<$some_database") or die "Could not open $some_database: $!";
while (chomp ($_ = <EML>)) {
if (/^\Q$in{'Email_Field'}\E$/) {
$found = 1;
last;
}
next;
}
close EML;

...then it is just a matter of checking whether $found equals 1 or 0. If it is 0 you can print the email to the file, otherwise show an error...eg....

$found ? error() : print_to_file();

Last edited by:

Paul: Jun 14, 2002, 10:43 AM
Quote Reply
Re: [Paul] form script modification In reply to
Paul, again - you amaze me with the speed of your responses.

I have finally worked out the syntax - and the script runs error free, but my 'if' statement is off - because it is not returning the appropriate error and continues to send to duplicate addresses.

# _________________________________________________________
sub CheckDuplicate {
my $found = 0;
open (EML, "<$database") or die "Could not open $database: $!";
while (chomp ($_ = <EML>)) {
if (/^\Q$in{'Email'}\E$/) {
$found = 1;
last;
}
next;
}
close EML;

if ($found eq 1) {&DoDuplicateError;}

}
sub DoDuplicateError {
print "Location: $DuplicateErrorPage\n\n";
exit;
}
# _________________________________________________________
Quote Reply
Re: [shiner] form script modification In reply to
Try changing eq to == ...you need == for numeric comparisons.
Quote Reply
Re: [Paul] form script modification In reply to
ok - that did not do it - the script still is sending the message to an address listed in the database.

The form element name is 'Email' and the variable in the script is $Email and is set up by

$Email = $in{'Email'};

so is this line correct?

if (/^\Q$in{'Email'}

The reason I ask is to see if it is compairing the right item to the address listed in the database. I also tried with no luck with:

if (/^\Q$in{'$Email'}

The second part would be to have it add an address to the database if $found=0

if ($found == 1) {&DoDuplicateError;}
else <write 'Email' in $database>
Quote Reply
Re: [shiner] form script modification In reply to
What is the format of your database?....it should work with a list of emails like:

me@here.com
foo@bar.com
doo@dar.com

Try printing $in{Email} to see what the value is and double check it exists in the database, then print $found and just compare....I'll test it out now to double check it does actually work Blush
Quote Reply
Re: [Paul] form script modification In reply to
it is just a text file I made in notepad - the only entry is my e-mail address. I called it bio.db

if you open it, there is one line - and on that line is chris@investingdd.com

http://www.investingdd.com/cgi-bin/form/bio.db

Last edited by:

shiner: Jun 14, 2002, 11:53 AM
Quote Reply
Re: [shiner] form script modification In reply to
You can see a working example here:

http://213.106.15.150/...gi?email=foo@bar.com

Code:

http://213.106.15.150/cgi-bin/test.cgi?code=1

Last edited by:

Paul: Jun 14, 2002, 12:04 PM
Quote Reply
Re: [Paul] form script modification In reply to
Paul,

I guess I am not successful at looking at the txt file. I have renamed my file emails.txt, and uploaded your test.cgi

http://www.investingdd.com/...hris@investingdd.com

it says that the address is not there, but it is in emails.txt

http://www.investingdd.com/cgi-bin/form/emails.txt

do I need to give it a physical path to the emails.txt file?
Quote Reply
Re: [shiner] form script modification In reply to
>>
do I need to give it a physical path to the emails.txt file?
<<

Yeah a full path, but if it couldn't find the file it should die.
Quote Reply
Re: [Paul] form script modification In reply to
ok -

I am sorry to take up your evening with something so minor. I simply do not understand why it is not finding it - if it is reading the file and the address is there.

Also,

how do I get it to write new addresses to the txt file?
Quote Reply
Re: [Paul] form script modification In reply to
Quote:
Yeah a full path, but if it couldn't find the file it should die.


No, you've got a precedence problem:

open FH, "emails.txt" || die "Cannot read emails: $!";

equals:

open FH, ("emails.txt" || die "Cannot read emails: $!");

Change that to:

open FH, "emails.txt" or die "Cannot read emails: $!";

and you'll probably see that it can't find the file. Also, you might want to replace:

/^\Q$email\E$/ and $found = 1, last;

with:

($email eq $_) and $found = 1, last;

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] form script modification In reply to
I always thought the precedence was:

open FH, $file || die $!;

or

open (FH, $file) or die $!;

...not sure why I thought that. Looking at it now, I can see why it needs to be the other way around...doh.

Last edited by:

Paul: Jun 14, 2002, 2:16 PM
Quote Reply
Re: [Alex] form script modification In reply to
Thanks guys!

For grins, I added some additional records to the emails.txt file. Surprisingly - it will find anything but the last record. That is why I could not find it earlier - as the only (and last) record, it could not be found. Any ideas on that one?

Also - how do I write new e-mail address to that data base before it exits?
Quote Reply
Re: [shiner] form script modification In reply to
I'm not sure why it won't find the last record...it still seems to work for me with one email in the database :(

Did you add a new line after the address at all?

foo@bar.com<return>

Last edited by:

Paul: Jun 15, 2002, 2:44 AM
Quote Reply
Re: [Paul] form script modification In reply to
The <return> was the key to solving that - I had no additional lines in the text file. Thanks!

Now, is there a simple command to add to the if statement to

if ($found == 1) {&DoDuplicateError;}
else <write 'Email' in $database>
Quote Reply
Re: [shiner] form script modification In reply to
$found ? DoDuplicateError() : WriteEmail();

Then:
Code:
sub WriteEmail {
#----------------------------------------------------
# Write the email to the database.

open (EML, ">> $database") || die "Cannot open $database: $!";
flock (EML, 2) unless $^O eq 'MSWin32';
print (EML, "$in{Email}\n");
close (EML);

return;
}

Last edited by:

Paul: Jun 15, 2002, 5:22 AM
Quote Reply
Re: [Paul] form script modification In reply to
The script works fine for checking duplication, however I now get an internal server error with the WriteEmail subroutine. I commented out the lines referring to it - and it works - so there is something in there it does not like.




sub CheckDuplicate {
my $found = 0;
open FH, "emails.txt" or die "Cannot read emails: $!";
while (chomp ($_ = <FH>)) {
($Email eq $_) and $found = 1, last;
}
close FH;
if ($found == 1) {&DoDuplicateError;}
# $found ? DoDuplicateError() : WriteEmail();
}


sub DoDuplicateError {
print "Location: $DuplicateErrorPage\n\n";
exit;
}

sub WriteEmail {
# Writes the email to the database.
open (EML, ">> $database") || die "Cannot open $database: $!";
flock (EML, 2) unless $^O eq 'MSWin32';
print (EML, "$in{Email}\n");
close (EML);
return;
}



Last edited by:

shiner: Jun 15, 2002, 8:12 AM
Quote Reply
Re: [shiner] form script modification In reply to
Damn, I'm always doing that...change:

print (EML, "$in{Email}\n");

to

print EML "$in{Email}\n";

Infact I got that error about half an hour ago with a script I'm writing hehe.

Last edited by:

Paul: Jun 15, 2002, 8:11 AM
Quote Reply
Re: [Paul] form script modification In reply to
That cleared the error, and you are sent to the success page - but nothing is written to the txt file.






sub CheckDuplicate {
my $found = 0;
open FH, "emails.txt" or die "Cannot read emails: $!";
while (chomp ($_ = <FH>)) {
($Email eq $_) and $found = 1, last;
}
close FH;
#if ($found == 1) {&DoDuplicateError;}
$found ? DoDuplicateError() : WriteEmail();
}


sub DoDuplicateError {
print "Location: $DuplicateErrorPage\n\n";
exit;
}

sub WriteEmail {
# Writes the email to the database.
open (EML, ">> $database") || die "Cannot open $database: $!";
flock (EML, 2) unless $^O eq 'MSWin32';
print EML "$in{Email}\n";
close (EML);
return;
}



Quote Reply
Re: [shiner] form script modification In reply to
Try using $Email ...it seems you may need to use that.
Quote Reply
Re: [Paul] form script modification In reply to
no effect, but I do think it needed the $




sub WriteEmail {
# Writes the email to the database.
open (EML, ">> $database") || die "Cannot open $database: $!";
flock (EML, 2) unless $^O eq 'MSWin32';
print EML "$in{$Email}\n";
close (EML);
return;
}



Quote Reply
Re: [shiner] form script modification In reply to
No it didn't otherwise you are trying to print a hash value with the email address as the key which won't work :)