Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing form data to a file

Status
Not open for further replies.

random26069

IS-IT--Management
Aug 14, 2010
1
US
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!
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top