Click to See Complete Forum and Search --> : Need help with simple Perl Script !!
Colfurby
09-22-2006, 03:06 AM
Hey guys i am really new to Perl and it took me ages just to figure out how to configure my web server to be able to access my scripts.
I just did the following html document and was wondering if you guys could help me out... i basically need a perl script to do the following.
1. Create a Feedback page to show the users what he has typed in the two fields.
2. Record the information from the form into a text file.
Really hope you guys can help me out with this !!
Thanks !
Here's the form ....
<html>
<head>
<title>My Form</title>
</head>
<body>
<form method="POST" action="/scripts/contactform.pl">
<p>Name:* <br>
<input type="text" name="Name">
<p>Comments<br>
<textarea type="text" name="textarea"></textarea>
<p>
<input type="submit" name="submit" value="Submit">
</form>
<p>
</body>
</html>
lmayer
09-22-2006, 03:16 PM
Being a new perl user myself I feel your pain
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $name = param('Name');
my $text = param('textarea);
# HTTP HEADER
print "Content-type: text/html \n\n";
print $name;
print $text;
That might help get you started. I've only used databases so I can't help you with the text file. But if you go here : http://www.cgi101.com/book/ and look at chapter 6.
It was a great help to me to get started.
Laura
Watts
09-22-2006, 03:26 PM
Cobbled together from several snippets I keep on hand....
#! path to perl
########## read form input and put into variables #########
read(STDIN, $strIN, $ENV{"CONTENT_LENGTH"});
@pairs = split(/&/, $strIN);
foreach $pair (@pairs) {
local($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$FORM{$name} = $value;
}
#you can now print $FORM{'whatever your field name is goes here'};
############ Create Text File ###########################
open(TEMP, ">>bob.txt") or die "Nada Can Do \n";
select(TEMP);
print qq|NAME IS: $FORM{'Name'} MESSAGE IS: $FORM{'textarea'} \n\n|;
close(TEMP);
#use ">>bob.txt" to append (add to) the text file
#use ">bob.txt" to overwrite the text file everytime
########### Display Message ############################
print "Content-Type: text/html;\n\n";
print qq|<HTML><HEAD><TITLE></TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<P>
<P>
<CENTER>
Dear $FORM{'Name'}, thank you for giving us your feed back of:
<BR> $FORM{'textarea'}.
</CENTER>
</BODY>
</HTML>
|;
You could get creative and assign some kind of counter to a variable and then instead of "bob.txt" you could do "$counter.txt" so everyone would get their own personal txt file.
<edit>Laura's is example much cleaner :) </edit>
Colfurby
09-23-2006, 03:56 PM
Cobbled together from several snippets I keep on hand....
#! path to perl
########## read form input and put into variables #########
read(STDIN, $strIN, $ENV{"CONTENT_LENGTH"});
@pairs = split(/&/, $strIN);
foreach $pair (@pairs) {
local($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$FORM{$name} = $value;
}
#you can now print $FORM{'whatever your field name is goes here'};
############ Create Text File ###########################
open(TEMP, ">>bob.txt") or die "Nada Can Do \n";
select(TEMP);
print qq|NAME IS: $FORM{'Name'} MESSAGE IS: $FORM{'textarea'} \n\n|;
close(TEMP);
#use ">>bob.txt" to append (add to) the text file
#use ">bob.txt" to overwrite the text file everytime
########### Display Message ############################
print "Content-Type: text/html;\n\n";
print qq|<HTML><HEAD><TITLE></TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<P>
<P>
<CENTER>
Dear $FORM{'Name'}, thank you for giving us your feed back of:
<BR> $FORM{'textarea'}.
</CENTER>
</BODY>
</HTML>
|;
You could get creative and assign some kind of counter to a variable and then instead of "bob.txt" you could do "$counter.txt" so everyone would get their own personal txt file.
<edit>Laura's is example much cleaner :) </edit>
thanks a bunch guys !!