CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
basic submit to text file script

 

First page Previous page 1 2 Next page Last page  View All


novice1
Novice

Jun 28, 2002, 11:10 PM

Post #1 of 28 (5251 views)
basic submit to text file script Can't Post

Hi ~ I'm new here and to perl!

Where might I be able to find a script that would process a form to a text file, no need for email..

Windows apache server.

Thanks!


davorg
Thaumaturge / Moderator

Jun 29, 2002, 12:47 AM

Post #2 of 28 (5243 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post


Code
#!/usr/bin/perl -Tw 

use strict;
use CGI qw(param header);
use Fcntl ':flock';

open STORE, '>/path/to/your/file' or die $!;
flock STORE, LOCK_EX or die $!;

foreach my $p (param) {
foreach my $v (param($p)) {
print STORE "$p : $v\n";
}
}

close STORE;

print header;
print <<END_HTML;

# put the HTML you want for the confirmation page here

END_HTML


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jun 29, 2002, 11:32 AM

Post #3 of 28 (5237 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

Dave thanks for that.

I have never coded, and never set a form up w/a script yet..

There are really only 3 things that need to be modified w/that yes?

1) Path to perl - which I think just '!perl -Tw' will work on this system.
2) Path to file '>c:/folder/folder/folder/file.txt'
3) HTML - 'Message Received.'

Do I leave the quotes in for #2? Don't have them for 1&2, just used them here.

Thanks, really appreciate it.


davorg
Thaumaturge / Moderator

Jun 29, 2002, 11:36 PM

Post #4 of 28 (5235 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Yep. All of your assumptions are correct.

The other part you might need to change is the part that actually writes the data to the file. The format is currently very simple and you might need something a bit more complex.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jun 30, 2002, 2:16 AM

Post #5 of 28 (5232 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

Dave,

It works, amazing.

You're right, now I see that each write to the file erases the previous information stored. How to append each submission to the next.

And what about including date/time submitted?

And including the names of the fields??

And what if rather than repling with an html statement, what about just serving an html file that's preworded - seems easier to control the font size/page colors that way.

Probably end up passing the fields over to another page in the near future for a confirmation anyway, then submit..

The data file needs to be created and on the server prior submission? I was thinking, is it possible, as maybe an alternative to appending each submission to the same data file, could the script generate a new text file and name it from a field (Name) from the form? Hmmmm..

Hey I really appreciate you helping out.

------
other notes:
I had removed the '#' from the first line as I thought was proper but got a server error message (which was better than the browser attempting to download a file).. So placed it back in and it then worked fine.

And from my previous message:
"Do I leave the quotes in for #2? Don't have them for 1&2, just used them here."
Meant to say, "Don't have them for 1&3, just used them here."
---


novice1
Novice

Jun 30, 2002, 3:10 AM

Post #6 of 28 (5230 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Dave - someone had recommended this script the other day from another newsgroup.. I couldn't get it going, of course that # missing on the first line...

Anyway, now that it is working - it does append each submission to the previous in the text file, but doesn't have the time...

I can't get it to view/display the results either. And what can I use for the $link since no hostname is avaiable. I was thinking just a reference to the location of the actual html file on the server..

I don't know, but these guys I assume are atheists? Hey anyway, so these atheists start off with okay comments, but towards the end of the file there are none...
---

#! /usr/local/bin/perl

##################################################################
# TC-Form.cgi is a modification of quickform.cgi written by Leif M. Wright.
# Quickform.cgi originally does not send an email but I needed a small script that did
# notify me whenever new data was added to a file. I added a simple e-mail procedure to
# make this happen.
#
# Most credit still goes to Leif M. Wright....
#
# Jaco Benard
# TECH-Concepts
# jbenard@tech-concepts.com
#
##################################################################
#


# this tells the program where to place a link for the user
# once they are done.
$link ="http://www.conservatives.net/atheist/";

# this is where the info will be written to - you need to specify a real directory
$file ="/usr/home/leif/public_html/conservatives/atheist/scripts/results.txt"; #must be read/writable

#
##################################################################

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;
$contents{$name} = $value;

}
}

chop($date = `date`);

# Now with the program
###########################################################
# Has to output a Content-type
print "Content-type: text/html\n\n ";

# Check to see if all required information was entered
# If you want a field to be required, add it here.
&no_cigar unless $contents{'name'};
&no_cigar unless $contents{'street'};
&no_cigar unless $contents{'city'};
&no_cigar unless $contents{'state'};
&no_cigar unless $contents{'zip'};
&no_cigar unless $contents{'country'};


sub no_cigar
{
print <<"HTML";
<HTML><HEAD><TITLE>Form Incomplete</TITLE></HEAD>
<BODY>
<H1>Form Incomplete</H1>
I'm sorry, the form was not filled completely.<br>
Please Return to the form and fill it out completely.<p>
Thank you.
<HR>
<a href=\"$contents{'url'}\">Return to the $contents{'formname'} page</a>
</BODY></HTML>
HTML
exit;
}



# They go here if the form was submitted
# successfully. Now this page will send them
# off to where ever specify in the "link" field above.

print <<"HTML";
<HTML><HEAD><TITLE>Entry successful</TITLE></HEAD>
<BODY>
<H1>Entry successful!</H1>
<p>
<hr noshade>
<p>
<H2>$contents{'name'}, I have received your form!</H2>
Your information will be processed immediately,
<b>$contents{'name'}</b>. Thank you.

<p>
<HR noshade>
<A HREF=\"$link\">Back to the home page</A>.
<a href="results.txt">View this form's results</a>.
</BODY>
</HTML>
HTML
#print "Content-type: text/plain\n\n ";
open(OUTPUT, ">>$file");
print OUTPUT "_______________________________\n";
print OUTPUT "Date: $date\n";
print OUTPUT "FORM NAME: $contents{'formname'}\n";
print OUTPUT "NAME: $contents{'name'}\n";
print OUTPUT "EMAIL: $contents{'email'}\n";
print OUTPUT "STREET: $contents{'street'}\n";
print OUTPUT "CITY: $contents{'city'}\n";
print OUTPUT "STATE: $contents{'state'}\n";
print OUTPUT "POSTAL CODE: $contents{'zip'}\n";
print OUTPUT "COUNTRY: $contents{'country'}\n";
print OUTPUT "PHONE: $contents{'phone'}\n";
print OUTPUT "COMMENTS: $contents{'comments'}\n";


close (OUTPUT);

open (MAIL, "| /usr/lib/sendmail -oi -n -t" );
print MAIL <<MAIL_MESSAGE;
To:you\@yourdomain.com
From:yourform\@yourdomain.com

You just received a new order...
MAIL_MESSAGE

close MAIL;



exit;


davorg
Thaumaturge / Moderator

Jun 30, 2002, 4:08 AM

Post #7 of 28 (5229 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post


In Reply To
Dave,

It works, amazing.

Not that amazing I hope. I like to think that most of my code works :)

In Reply To
You're right, now I see that each write to the file erases the previous information stored. How to append each submission to the next.

Replace the line that opens the file with

Code
open STORE, '>>/path/to/your/file' or die $!;

'>' means overwrite file, '>>' means append to end of file.


In Reply To
And what about including date/time submitted?

Simplest solution is to add

Code
my $time = localtime;

within the print look and then add $time to the data that you print.

In Reply To
And including the names of the fields??

Doesn't it already do that?

In Reply To
And what if rather than repling with an html statement, what about just serving an html file that's preworded - seems easier to control the font size/page colors that way.

Replace the "print header" and "print <<END_HTML" lines with the single line

Code
print redirect('http://yourserver.com/some_page.html')'


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 2, 2002, 2:08 PM

Post #8 of 28 (5222 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

D-
Amazing in that I have it working now..
Your code seems far cleaner than the other.

Got the append.
Could you give me an example of where this time goes, that's got me.
Yeah it does include the field names, don't remember why I brought that up.
What if the print redirect has no domain name, just ip and port.

And what about optional required fields, generating a little message.


Appreciate it D - it's all coming together.


novice1
Novice

Jul 9, 2002, 10:05 AM

Post #9 of 28 (5206 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Dave -

Your script is sweet man - I've set up the response/result page in html, fonts and colors and positioning are all dialed in.

Just a few more details:

1) How to name the result window. Be nice to not have the path and script name displayed.
2) How to add a separator - line or space to the text file between each write/submission.
3) What about dropping one of the fields back into the response, such as the email.
4) Inserting that time into the script is throwing me...
5) Is it difficult to validate fields. Would like to just confirm an entry is present in certain fields, if not then generate/serve another message.

Your script is so much cleaner and easier to maintain. The atheist ppl, w/theirs EVERY field on the form needed to be entered - that's work if implemented for multiple forms - and it wasn't processing the checkboxes and radio buttons.

In the meantime I'm reading sites on perl.

Thanks for everything !


davorg
Thaumaturge / Moderator

Jul 10, 2002, 4:52 AM

Post #10 of 28 (5200 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post


In Reply To
Dave -

Your script is sweet man - I've set up the response/result page in html, fonts and colors and positioning are all dialed in.

Just a few more details:

1) How to name the result window. Be nice to not have the path and script name displayed.


That's just a case of adjusting the HTML you display. You probably need a <title> ... </title> tag.


In Reply To
2) How to add a separator - line or space to the text file between each write/submission.


I've added that in the new version below. It's the line that says

print STORE '-' x 70, "\n";


In Reply To
3) What about dropping one of the fields back into the response, such as the email.


In the version below, put the names of fields you want to display to the user in @show. You'll then end up with a variable called $fields which you can put in your HTML somewhere. This contains a table of fields and their values.


In Reply To
4) Inserting that time into the script is throwing me...


That's now in the new version too. The line saying:

print STORE 'Data submitted at: ', scalar localtime, "\n";


In Reply To
5) Is it difficult to validate fields. Would like to just confirm an entry is present in certain fields, if not then generate/serve another message.


Also added in the latest version. Put the names of your mandatory fields in @mandatory.

Here's the new (improved) version:

Code
#!/usr/bin/perl -Tw 

use strict;
use CGI qw(param header ul li table Tr td);
use Fcntl ':flock';

# List of fields that must be filled in.
# If any of these fields are blank then an error message
# will be generated
my @mandatory = qw(field1 field2);

# List of fields to show in the output
my @show = qw(field3 field4);

my @errs;
foreach (@mandatory) {
push @errs, $_ unless param($_);
}

if (@errs) {
my $err = ul(li([@errs]));

print header;
print <<END_HTML;

# Insert HTML that tells the user they've made an error and missed out
# mandatory fields. Use the variable $err somewhere in the HTML as this
# contains a list of the missing fields

END_HTML
# exit the program
exit;
}

open STORE, '>>/path/to/your/file' or die $!;
flock STORE, LOCK_EX or die $!;

print STORE 'Data submitted at: ', scalar localtime, "\n";

foreach my $p (param) {
foreach my $v (param($p)) {
print STORE "$p : $v\n";
}
}

print STORE '-' x 70, "\n";

close STORE;

$rows .= Tr(td($_), td(param($_))) foreach @show;
my $fields = table($rows);

print header;
print <<END_HTML;

# Put the HTML you want for the confirmation page here.
# Within the HTML you can include the variable $fields which
# contains a table of the fields from @show

END_HTML


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 11, 2002, 9:15 AM

Post #11 of 28 (5195 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

D -

Is this the correct format?

# List of fields that must be filled in.
# If any of these fields are blank then an error message
# will be generated
my @mandatory = qw(name);

# List of fields to show in the output
my @show = qw(email);

getting a premature end of script header error.
and if no mandatory field, to leave blank then just ()??
and it's not required that the variables $err and $fields be dropped into the response right

Thanks man!


davorg
Thaumaturge / Moderator

Jul 11, 2002, 9:33 AM

Post #12 of 28 (5194 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post


In Reply To
D -

Is this the correct format?

# List of fields that must be filled in.
# If any of these fields are blank then an error message
# will be generated
my @mandatory = qw(name);

# List of fields to show in the output
my @show = qw(email);

Looks fine to me.


In Reply To
getting a premature end of script header error.

There will be a more helpful error message in the web server error log.


In Reply To
and if no mandatory field, to leave blank then just ()??

Yep

In Reply To
and it's not required that the variables $err and $fields be dropped into the response right


Nope, it's not required. But it's what they're there for :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 11, 2002, 10:22 AM

Post #13 of 28 (5192 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

Yes the w switch..

Okay I reinstalled the script. Only modified the path to Perl, path to text file, eliminated entry for mandatory and show ().

This from the error log:

Global symbol "$p" requires explicit package name at script.pl line 24.
Global symbol "$v" requires explicit package name at script.pl line 24.
Global symbol "$p" requires explicit package name at script.pl line 24.
Global symbol "$p" requires explicit package name at script.pl line 24.
Global symbol "$v" requires explicit package name at script.pl line 24.
Global symbol "$rows" requires explicit package name at script.pl line 24.
Global symbol "$fields" requires explicit package name at script.pl line 24.
Global symbol "$rows" requires explicit package name at script.pl line 24.
Global symbol "$fields" requires explicit package name at script.pl line 24.
Missing right curly or square bracket at script.pl line 61, at end of line
(Might be a runaway multi-line << string starting on line 24)
script.pl has too many errors.

I haven't read how to count the lines yet, so not sure exactly where it is referring.


davorg
Thaumaturge / Moderator

Jul 12, 2002, 12:54 AM

Post #14 of 28 (5187 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Can you repost the entire script exactly as you currently have it?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 12, 2002, 3:24 PM

Post #15 of 28 (5184 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

here you go D-

---

#!perl -Tw

use strict;
use CGI qw(param header ul li table Tr td);
use Fcntl ':flock';

# List of fields that must be filled in.
# If any of these fields are blank then an error message
# will be generated
my @mandatory = qw();

# List of fields to show in the output
my @show = qw();

my @errs;
foreach (@mandatory) {
push @errs, $_ unless param($_);
}

if (@errs) {
my $err = ul(li([@errs]));

print header;
print <<END_HTML;

# Insert HTML that tells the user they've made an error and missed out
# mandatory fields. Use the variable $err somewhere in the HTML as this
# contains a list of the missing fields

END_HTML
# exit the program
exit;
}

open STORE, '>>/path/path/file.txt' or die $!;
flock STORE, LOCK_EX or die $!;

print STORE 'Data submitted at: ', scalar localtime, "\n";

foreach my $p (param) {
foreach my $v (param($p)) {
print STORE "$p : $v\n";
}
}

print STORE '-' x 70, "\n";

close STORE;

$rows .= Tr(td($_), td(param($_))) foreach @show;
my $fields = table($rows);

print header;
print <<END_HTML;

# Put the HTML you want for the confirmation page here.
# Within the HTML you can include the variable $fields which
# contains a table of the fields from @show

END_HTML


davorg
Thaumaturge / Moderator

Jul 12, 2002, 10:54 PM

Post #16 of 28 (5180 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Well, there was one error I needed to fix. Add the line

Code
my $rows;

before

Code
$rows .= Tr(td($_), td(param($_))) foreach @show;

But I'm not getting the other errors you're seeing. Are you sure you've shown us the code that gives the errors. The errors you're getting seem to imply that you've added some HTML to the section starting on line 24 - but there's none in the code that you posted.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 13, 2002, 12:44 AM

Post #17 of 28 (5178 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

I ran it again, added the line as you said.

Same error message, only difference is now the missing curly or right square bracket is at line 62 - last time it said line 61.

I haven't dropped in the html response for this one yet, it is an exact duplicate of what I posted + your modification suggestion..


davorg
Thaumaturge / Moderator

Jul 13, 2002, 5:45 AM

Post #18 of 28 (5177 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Ah, got it.

The problem is almost certainly that the "END_HTML" isn't being recognised. Each time you have an "END_HTML" string on its own on the line, it must be the _only_ thing on the line. There must be no spaces before of after it.

And for the one that is at the end of the program, you need to ensure that there is at least one blank line following it.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 13, 2002, 7:22 AM

Post #19 of 28 (5175 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

Excellent - works now.

Seems that font formatting (color, size, type) works when applied to the $err but not to the $fields. If this is an html issue, strange because I'm copying the code from $err for the $fields.

Other than that, it's all set for now.


novice1
Novice

Jul 15, 2002, 4:47 PM

Post #20 of 28 (5167 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

What about including the IP address in the submission..

And also, the fields that are displayed on the response page for $err - slight issue because if the fieldsnames have spaces, then that formatting conflicts with the method for listing them in the script. I don't have a problem with replacing the spaces in the names with underscores, but then if those field names are required and then generated in the reponse page - they appear as field_1_etc..

For the future - is it very difficult to generate a preview page prior to submission?


And, dropped in a link, 'back to form' on the error page.. Brings back up the form, but data not present previously entered and generates a browser error when submitted again..


davorg
Thaumaturge / Moderator

Jul 16, 2002, 1:31 AM

Post #21 of 28 (5162 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post


In Reply To
What about including the IP address in the submission..


It's stored in the variable $ENV{REMOTE_ADDR}.


In Reply To
And also, the fields that are displayed on the response page for $err - slight issue because if the fieldsnames have spaces, then that formatting conflicts with the method for listing them in the script. I don't have a problem with replacing the spaces in the names with underscores, but then if those field names are required and then generated in the reponse page - they appear as field_1_etc..


That's a problem with the way we define the @mandatory array. If you want spaces in the fieldnames, then you can define it like this instead:

Code
my @mandatory = ('field one', 'field two');

The same is true of the @shown array.


In Reply To
For the future - is it very difficult to generate a preview page prior to submission?


I guess it could be done. But it would make the program quite a lot more complex.


In Reply To
And, dropped in a link, 'back to form' on the error page.. Brings back up the form, but data not present previously entered and generates a browser error when submitted again..


Yeah, that's the way that it would work I'm afraid. You could try adding a button with some Javascript like this:

Code
<input type="button" 
value="&lt;&lt; Go Back"
onclick="history.go (-1)">


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 16, 2002, 8:52 PM

Post #22 of 28 (5157 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

my @mandatory = ('field one', 'field two');

Isn't happy, even w/the commas. Still sees one field with a space as two fields..


davorg
Thaumaturge / Moderator

Jul 17, 2002, 1:43 AM

Post #23 of 28 (5153 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Define "isn't happy". What behaviour do you see that you don't expect?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


novice1
Novice

Jul 17, 2002, 3:50 AM

Post #24 of 28 (5151 views)
Re: [davorg] basic submit to text file script [In reply to] Can't Post

For example-

Field names are: (in the html form)

field 1
field 2

In the script they are referrenced in the @mandatory as

'field 1, field 2'

What is displayed on the response page by #err variable is

*field
*1
*field
*2


novice1
Novice

Aug 13, 2002, 9:36 PM

Post #25 of 28 (5125 views)
Re: [novice1] basic submit to text file script [In reply to] Can't Post

Hey Dave - does the script need anything additional to allow for a file to be sent/submitted?

T

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives