Hi all,
I am new to perl and this forum. I am trying to setup a mailing list subscription functionality for customers to receive a periodic newsletter from me. My perl program grabs the html form 'email address' text input but I am then having issues writing this data to a plain text file.
Below is my html form followed by my perl script. When i run this on IIS, i dont receive any errors, nor does anything get written to the file. Any help would be most appreciated. :) email.html - <html>
-
<body>
-
<FORM ACTION="/new/email.pl" METHOD="POST">
-
Email Address: <INPUT TYPE="text" NAME="email" size=30>
-
<br><br><INPUT TYPE="submit" VALUE="Sign up">
-
</FORM>
-
</body>
-
</html>
-
email.pl - print "Content-type: text/html\n\n";
-
-
#Read email address from Form
-
if ($ENV{'REQUEST_METHOD'} eq 'POST')
-
{
-
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); -
@pairs = split(/&/, $buffer);
-
foreach $pair (@pairs)
{ -
($name, $value) = split(/=/, $pair);
-
$value =~ tr/+/ /;
-
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
-
$FORM{$name} = $value;
-
}}
-
-
#Write to file
-
my $file = '/new/email.txt';
-
-
open (FILE, ">>" . $file) or die "cannot open file for appending: $!";
-
flock (FILE, 2) or die "cannot lock file exclusively: $!";
-
print FILE $FORM{email};
-
close (FILE) or die "cannot close file: $!";
-
Thanks again,
Mick
21 5771
hi,
try with this simple code,
definitely it will work 4 u, -
<html>
-
<body>
-
<FORM ACTION="/new/email.pl" METHOD="POST">
-
Email Address: <INPUT TYPE="text" NAME="email" size=30>
-
<br><br><INPUT TYPE="submit" VALUE="Sign up">
-
</FORM>
-
</body>
-
</html>
-
email.pl
----------- -
#!/usr/bin/perl
-
use CGI qw(:standard);
-
use Fcntl ':flock';
-
print "Content-type: text/html\n\n";
-
$a=param('email');
-
#Write to file
-
my $file = '/new/email.txt';
-
-
open (FILE, ">>" .$file) or die "cannot open file for appending: $!";
-
flock (FILE, 2) or die "cannot lock file exclusively: $!";
-
print FILE $a;
-
close (FILE) or die "cannot close file: $!";
-
You have posted code into the forum without using proper code tags. It is best practice here on TSDN to wrap all code posted into the forum in code tags.
Code tags start with [code] and end with [/code]. If you need an example other than this one, the please refer to the REPLY GUIDELINES (or POSTING GUIDELINES if you are starting a discussion) to the right of the Message window. You will find the examples and suggestions for posting there.
In addition, you can also add the language to your code tags. Here is an example:
[code=perl]
<some code>
[/code]
Please know that I have fixed your posts above to include the proper code tags. Please be sure and use code tags in all of your future posts here on TSDN. This applies to BOTH of you as neither of you used the tags!!
- Moderator
thanks for your quick response!! unfortunately, this still did not work. :( i ran it, entered an email address and a blank page came up with the status 'done'. still no write was made to the file.
so my .htm form has an email address entered and submitted and the output is a blank page. not sure what is up here. any other suggestions?
im running ActivePerl-5.8.8.820-MSWin32-x86-274739.msi on windows 2003 with IIS 6. dont know if this helps at all.
also, what is html 4 strict? do i need to change anything here?
cheers,
Mick
Well, I would make sure that the path to the file is correct. Then add some code for debuging. I don't believe that flock works on windows. I believe this is unix only.
Here is a script that will help you. I have added some code to check the email address to make sure that is validates. No need for a security hole:)
This code uses Email::Valid - #! /usr/bin/perl -T
-
-
use strict;
-
use warnings;
-
-
use CGI;
-
use CGI::Carp qw/fatalsToBrowser/;
-
use Email::Valid;
-
-
my $q = CGI->new;
-
-
print $q->header;
-
print $q->start_html(-title =>'Mysite.com');
-
-
my $email_file = 'h:/path/to/file/email_list.txt';
-
-
if (Email::Valid->address($q->param('email'))) {
-
&process_data($q->param('email'));
-
} else {
-
print 'Your email address does not appear to be valid';
-
}
-
-
sub process_data {
-
-
my ($email_to_log) = @_;
-
-
open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
-
print $EMAIL_LIST $email_to_log;
-
close $EMAIL_LIST;
-
-
print 'Go check your file to make sure it wrote correctly OR add some code to do it for you';
-
}
-
-
print $q->end_html();
-
-
1;
Modification to the code I posted.
Change: - print $EMAIL_LIST $email_to_log;
To: - print $EMAIL_LIST $email_to_log, "\n";
Otherwise all of your email address' will end up on the same line. Also, the code I posted does not take into account duplicates.
Make sure you are using the proper path to perl or else you will get a 5OO error.
Widnow's will be something like C:\perl\bin\perl.exe
This line can be changed to your site's title, just change the wording "Mysite.com". - print $q->start_html(-title =>'Mysite.com');
Hi,
Thanks again for your suggestion as you have been most helpful. I thought that in order to prevent any further pain for you :), I would post here my exact code and system set up as it currently stands to see whether you can identify any issues.
Setup
-Currently my server is Win2003 with IIS 6.0
-The 3 files email.pl, email.html and email.txt all reside in C:\cgi-bin
-My perl install is located in C:\perl\bin\perl.exe as you mentioned
My code is currently as follows: email.html -
<html>
-
<body>
-
<FORM ACTION="/cgi-bin/email.pl" METHOD="POST">
-
Email Address: <INPUT TYPE="text" NAME="email" size=30>
-
<br><br><INPUT TYPE="submit" VALUE="Sign up">
-
</FORM>
-
</body>
-
</html>
-
email.pl -
#! C:\perl\bin\perl.exe
-
-
use strict;
-
use warnings;
-
-
use CGI;
-
use CGI::Carp qw/fatalsToBrowser/;
-
use Email::Valid;
-
-
my $q = CGI->new;
-
-
print $q->header;
-
print $q->start_html(-title =>'mysite.com.au');
-
-
my $email_file = 'c:/cgi-bin/email.txt';
-
-
if (Email::Valid->address($q->param('email'))) {
-
&process_data($q->param('email'));
-
} else {
-
print 'Your email address does not appear to be valid';
-
}
-
-
sub process_data {
-
-
my ($email_to_log) = @_;
-
-
open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
-
print $EMAIL_LIST $email_to_log;
-
close $EMAIL_LIST;
-
-
print 'Go check your file to make sure it wrote correctly OR add some code to do it for you';
-
}
-
-
print $q->end_html();
-
-
1;
-
-Also, to use Email::Valid, do I need to install/cfg anything further than the standard perl.
-For the IIS web extensions, is it advisable to use the .exe or .dll?
Cheers,
Mick
You will need to install Email::Valid which can be done using PPM. I use XAMPP which is an Apache Web Server environment (for development only) on a windows machine. For me the path to perl is as I showed you above. I am not certain how it is setup on Win2003 with IIS 6.0.
Do you still get error when you run the script? If so, then maybe this link can help you.
Kevin
i think im getting closer now. your "Go check your file to make sure it wrote correctly......" line appears after i submit the email address. however still nothing written to file. when using the -w flag, i receive the following warning: print() on closed filehandle $EMAIL_LIST
The warning indicates that the offending line of code is: -
print $EMAIL_LIST $email_to_log;
-
i have tried printing the html form input back to the screen which works correctly so it must be in the file write code??
thanks
mick
I tested the exactly code that you have posted and it worked for me. It work to the file correctly. To me it sounds like the path is not correct.
Make the following modifications to your code and see what happens. As far as the -w. That is not needed it has already been done with the use warnings. - print "$ENV{'DOCUMENT_ROOT'}/NEW_____email.txt\n";
-
my $email_file = "$ENV{'DOCUMENT_ROOT'}/NEW_____email.txt";
Once you confirm the path the change it back.
yep, the path is correct. i am using an absolute reference in my .pl script below:
now the program writes '0' to file. anything i type in the textfield comes up as a zero in the txt file.
if i replace the below line
with - my $email_to_log = "test";
, i can get 'test' printed to the file. -
#!C:\perl\bin
-
-
use strict;
-
use warnings;
-
use CGI;
-
use CGI::Carp qw/fatalsToBrowser/;
-
-
my $q = CGI->new;
-
print $q->header('text/html');
-
print $q->start_html(-title =>'Website Name');
-
-
my $email_file = 'c:/cgi-bin/email.txt';
-
-
&process_data($q->param('email'));
-
-
sub process_data{
-
my $email_to_log = @_;
-
open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
-
print $EMAIL_LIST "$email_to_log\n";
-
close $EMAIL_LIST;
-
}
-
-
print $q->end_html();
-
-
1;
-
i have just stripped the code write back so the email::validate content is removed however the above script does not receive compilation errors.
cheers,
mick
Change:
To:
The reason is without the () you are getting the number of elements in the array @_. With the () you are getting the contents of the element(s) of the array.
ok, yes that makes sense. however, if that is the case, should there not be a '1' entered into the file to indicate the one parameter (the email address) that was entered in the form?
i say this because when i remove the parethesis around that line you mention above, i receive a "email.pl: Use of uninitialized value in string at C:\cgi-bon\email.pl line 23" error. then a " " (space) is written to the file.
When I used the code from post #12 as is and used your form and I did get a 1 in the file. When I added the () like so:
I got the value of $q->param('email') as expected.
ok, that's an interesting point then. using the same code, a 0 or " " gets written to file, depending on the parenthesis being present or not. This is quite confusing as i can:
A) Write the html form content (once captured) back to the screen and
B) As mentioned before, write text such as 'test' to the file.
My program isnt doing both though....picking up the form input and then writing that form input to file.
I cant for the life of me think what the problem may be!?
replace this: - sub process_data{
-
my $email_to_log = @_;
-
open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
-
print $EMAIL_LIST "$email_to_log\n";
-
close $EMAIL_LIST;
-
}
-
with: - sub process_data{
-
open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
-
print $EMAIL_LIST $q->param('email'), "\n";
-
close $EMAIL_LIST;
-
}
and report back
hi KevinADC,
there are no compilation errors or warnings. however, when run nothing but a space gets printed to the file. it then goes to a newline.
any other suggestions?
would this have anything to do with components missing from my install or my IIS set up?
cheers,
mick
personally, I am suspecting that your form field name "email" is not what you really have in your form. In windows case is not significant, but in perl it is, if the form field name is "Email" (or whatever) you have to use that exact spelling in the perl script. Now if the form field name is correct, I am stumped, I don't know why the value of the form field is not printing to the file.
also, look for any error in the html code that might cause the form to break, such as a missing quote or a miss-spelled tag/attribute.
yep, my html file is good and I am running the same .pl file as posted. the file doesnt print any " ", just a newline each time.
well thanks for all your help everyone. i wont waste anymore of your time......im utterly confused as to y it wont work. time to go lick my wounds. :)
thanks
mick
Let's try to see if anything is coming from the form. - #!C:\perl\bin
-
use strict;
-
use warnings;
-
-
use CGI;
-
use CGI::Carp qw/fatalsToBrowser/;
-
-
my $q = CGI->new;
-
print $q->header('text/html');
-
print $q->start_html(-title =>'Website Name');
-
-
my $email_file = 'c:/cgi-bin/email.txt';
-
my $email_to_log = $q->param('email') || 'SomeEmail@email.com';
-
my $email_in = $q->param('email') || 'Field was empty'; #If the incoming is empty then this will also show us.
-
-
print "Value of \$email_to_log: $email_to_log<br>"; #This will tell if what email is being sent to the file. Also shows if the $q->param('email') contains anything.
-
print "Value of \$email_in: $email_in<br>"; #This will print the value of the textfield 'email';
-
-
-
open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
-
print $EMAIL_LIST $email_to_log, "\n";
-
close $EMAIL_LIST;
-
-
print "Go check your file and see what happened";
-
-
-
print $q->end_html();
Try this and see what you get.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michael Levin |
last post by:
I've got the following problem. I'm a biologist and I have a device at work
which monitors my frog habitat. The device has a bunch of sensors, and runs
an embedded html server with some java...
|
by: rxl124 |
last post by:
someone please please help w/ this one.
As I been working on this on and off and it just does not want to work.
1 #!/usr/bin/perl -w
2
3 $file = "/home/user1/dothis";
4 open(FILE, ">$file");...
|
by: von |
last post by:
I am writing data from a Javascript to a text file using a Perl script and
it all works pretty well - except ...
This:
"Here is my data"
becomes:
"Here%20is%20my%20data" when it gets to...
|
by: von |
last post by:
I am trying to write a single piece of data (that is generated from a
Javascript) to a text file on my server via a Perl script.
The Javascript is setup so that I can display the required data on...
|
by: Jean-David Beyer |
last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2
V6.1 (with all the FixPacks) on my old machine.
I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
|
by: rahulthathoo |
last post by:
Hi
I have my home directory on my departments server. Somehow I am not
able to write to a file using a php code,
$myFile = "trial3.txt";
$fh = fopen($myFile, "a+") or die("can't open file");
...
|
by: KevinADC |
last post by:
Note: You may skip to the end of the article if all you want is the perl code.
Introduction
Uploading files from a local computer to a remote web server has many useful purposes, the most...
|
by: Just_a_fan |
last post by:
I tried to put an "on error" statement in a routine and got the message
that I cannot user "on error" and a lamda or query expression in the
same routine.
Help does not list anything useful for...
|
by: happyse27 |
last post by:
Hi All,
my html code is sno 1) and perl code is sno 2).
a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |