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 5822
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: $!";
-
numberwhun 3,509
Recognized Expert Moderator Specialist
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
eWish 971
Recognized Expert Contributor
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;
eWish 971
Recognized Expert Contributor
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.
eWish 971
Recognized Expert Contributor
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\per l.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\per l.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
eWish 971
Recognized Expert Contributor
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
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 functions defined which know how to
read the hardware sensorts. I access it from wherever I am via any browser,
and it displays the measurements (a set of simple numbers) as Java applets
in a simple html page. One entry in this page (a table cell) looks...
|
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");
5 while($line = <FILE>) {
6 if ($line =~ /B/) {print FILE "A"};
7 if ($line =~ /A/) {print FILE "B"};
|
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 the text file.
|
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 my
website using the following HTML:
<span name='Data1' class='data1'></span>
But I need it sent to a text file. :(
|
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
Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After
creating an instance, starting the database, creating a database, and
entering the table definitions, all of which seems to work OK, I entered a
tiny 8-row table and can do...
| |
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");
Instead of a+ if i use r or w or anything, it does not work. Could this
be a permission thing? The entire directory had a 777 permission and
even i after i created a file and chmod'ed 777 to it, the code couldnt
open it. What could be the matter?...
|
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
|
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 explaining a "lamda" expression
and so I don't know what one is and I am not doing any database stuff in
the entire program.
So what does this error message really mean and what can I do to get an
On Error into the routine?
|
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 didnt display the html after the perl script executed. Using perl 5.1 and apache 2.2.9 version(apache installed and run without any errors and no warning, perl tested fine)
b) Also, when i clicked the html code to submit the upload of the...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |