×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

writing form data to a file

writing form data to a file

writing form data to a file

(OP)
Hi people, it's been a LONG time since I did any perl and I was wondering if someone could pease give me a quick example for the following so I don't have to digou books and do 10 hours research for a 5 minute job...

I need to create an HTML doc with two text boxes on it for the user to nter informtion into (if I remember correctly I can name them, I'll name them txtuname and txtemail).  Then I need a form button (submit?  Yes, been a long time lol).  Please include an example of he code for this HTML button to call the script.

The script needs to:
Check to make sure the txtuname field is not blank.
Make sure the txtemail field is a valid email address - if it is blank or NOT a valid email format  (address@domain.sub) then substitute the value "private" for the email address
Open a text file for APPEND
Write the name and email address to the end of the file, one piece of info per line so it is written asfllows:

uname1
uemail1
uname2
uemail2
etc.

Or, if you're feeling really ambitious, it can write it

uname1 uemail1 (or uname1,uemail1)
uname2 uemail2 (or uname2,uemail2)

as long as you include a snippet to remnd me how to get the individual name & email data into 2 separate javascript variables (off the top of my head I only remember line reading)

Thanks!
 

RE: writing form data to a file

Hey,

There are a few different ways this could be done. I've produced a very rough example. I'm unsure what you meant by the javascript part, but i'm sure you probably just want to put an onClick event on the submit button, and also submit the form using javascript.

CODE

#! /usr/bin/perl
use strict;
use CGI ':standard';
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
$CGI::POST_MAX = 10000;
use Cwd;
print "Content-type: text/html\n\n";
#####
# initial variables
my $thisscript = 'Script.pl';
my $outputfile = getcwd().'/Output.txt';
#####
# form data
my $submit = param('submit');
my $txtuname = param('txtuname');
my $txtemail = param('txtemail');
#
# cgi error check
my $q = CGI->new;
die 'cgi error.' if ($q->cgi_error);
#####
# process form data
my ($form_pass, $form_fail, $form_begin);
if ($submit) {
    $txtemail = ($txtemail =~ m/^[^@]{1,60}@[^@.]{1,60}(\.[^@.]{1,60})?(\.\w{2,6}){1,4}$/) ? $txtemail : 'private';
    if ($txtuname) {
        $form_pass = 1;
        open my $fh, '>>', $outputfile or die "cannot open $outputfile - $!";
        print $fh "$txtuname, $txtemail\r\n";
        close $fh;
    }
    else { $form_fail = 1; }
}
else { $form_begin = 1; }
#####
# header
print qq(
    <html>
        <head>
            <title></title>
        </head>
        <body>
);
#####
# message
if ($form_pass) {
    print qq(
        <p>Form submission passed.</p>
    );
}
elsif ($form_fail) {
    print qq(
        <p>Form submission failed.</p>
    );
}
elsif ($form_begin) {
    print qq(
        <p>Fill in the form below.</p>
    );
}
#####
# form
if ($form_fail or $form_begin) {
    print qq(
        <form method="post" action="$thisscript">
            <p>uname: <input type="text" name="txtuname" value="$txtuname" /></p>
            <p>email: <input type="text" name="txtemail" value="$txtemail" /></p>
            <p><input type="submit" name="submit" value="Post" /></p>
        </form>
    );
}
#####
# footer
print qq(
        </body>
    </html>
);

Chris

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close