PDA

View Full Version : Need help with simple HTML form post to file



ARhere
December 29th, 2011, 06:41 AM
Hello People-who-are-smarter-than-me. :D

I wanted a way for people visiting my server (with a simple HTML web page) to leave me simple one line text messages. I was hoping to use the HTML form command...

<p><form name="input" action="input_action_here" method="post">
Message: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

...and have the entered text written to a file called messages.txt similar to how the Linux redirect command works

echo "hello world" >> messages.txt
Sorry but I am an electrical engineer so forgive me if this is HTML 101. :P How can I easily do this?
-AR

nmaster
December 29th, 2011, 07:04 AM
do you have php installed on the web server? this covers a slightly more complicated problem, should definitely help:
http://www.dreamincode.net/forums/topic/9866-textarea-editor/

PS: im an EE person too :)

HotCupOfJava
December 29th, 2011, 07:11 AM
If you're wanting to write to a file, you're gonna need some sort of server-side language, as nmaster has indicated. Many (but not all) web hosting companies have php running even for their free accounts. Alternatively, you could use Javascript and have the form email you messages from the page.

nmaster
December 29th, 2011, 07:14 AM
if you do something with email (or really in any case) you might want to use reCAPTCHAs to prevent people/bots from screwin' with you:
http://www.google.com/recaptcha

Lars Noodén
December 29th, 2011, 12:25 PM
Here's a short perl script that reads a single value from HTML form data and then appends that value to a text file.



#!/usr/bin/perl

use CGI; # Common Gateway Interface
use strict; # restrict sloppy constructs
use warnings; # enable warnings

my $file = qq(/var/tmp/output.txt); # text output
my $query = CGI->new; # new CGI query

# HTTP headers and HTML head

print $query->header( -type=>"text/html", -charset=>"UTF-8" );
print $query->start_html( -title=>"Form output" );

# get values of one single form field (user)

my @values = $query->param('user');

open ( TAB, ">>", $file )
or die ( "Could not open '$file' for appending: $!\n" );

foreach my $value ( @values ) {
$value =~ tr/\000-\037/ /s; # remove control chars if any
print qq(v=),$value,qq(<br /> \n);
print TAB $value,"\n"; # output to file
}

close ( TAB );

# Done

print qq(</body>\n</html>\n);
exit ( 0 );

ARhere
December 31st, 2011, 08:36 PM
Thanks Everyone!

It looks like Perl was the best approach and then Lars Noodén was nice enough to post an example.

-AR

ARhere
December 31st, 2011, 08:52 PM
I jumped the gun on setting this "SOLVED" too soon.

I have the slightly modified Perl code saved as getmessage.pl in the same location as the index.html that is calling it. I tried calling the script directly in the action HTML field but this does not work.


<p><form name="input" action="perl ./getmessage.pl" method="post">
Message: <input type="text" name="usmr" />
<input type="submit" value="Submit" />
</form>

Lars Noodén
December 31st, 2011, 09:48 PM
Perl is already called by the line #!/usr/bin/perl so you don't have to call it again. So if your script is called getmessage.pl and is in the directory /usr/lib/cgi-bin then your form should look like this:



<p><form name="input" action="/cgi-bin/getmessage.pl" method="post">


That's assuming the defaults for Apache2.

ARhere
December 31st, 2011, 10:19 PM
Perl is already called by the line #!/usr/bin/perl so you don't have to call it again. So if your script is called getmessage.pl and is in the directory /usr/lib/cgi-bin then your form should look like this:



<p><form name="input" action="/cgi-bin/getmessage.pl" method="post">


That's assuming the defaults for Apache2.

Location issue, I had the getmessage.pl script in my /var/www/ location. Moving to /usr/lib/cgi-bin/ fixed it, thank you.

One last thing please. I would like to have the Perl script enter a line break, then current date+time, then the message. Just in case I forget to check the file for a while. When I get home I will open a Perl book to learn more. :D

Thanks again Lars,
-AR

Lars Noodén
December 31st, 2011, 10:29 PM
Then I would add a little to the script:



#!/usr/bin/perl

use CGI; # Common Gateway Interface
use strict; # restrict sloppy constructs
use warnings; # enable warnings

my $file = qq(/var/tmp/output.txt); # tab-delimited output
my $query = CGI->new; # new CGI query

# HTTP headers and HTML head

print $query->header( -type=>"text/html", -charset=>"UTF-8" );
print qq(<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Form output</title>
</head>
);

my ($s,$m,$h,$day,$mon,$year) = localtime();
$year += 1900;
$mon++;

# get values of one single form field (user)
my @values = $query->param('user');

open ( TAB, ">>", $file )
or die ( "Could not open '$file' for appending: $!\n" );

foreach my $value ( @values ) {
print qq(v=),$value,qq(<br /> \n);
print TAB qq($year-$mon-$day $h:$m:$s\t),$value,"\n";
}

close ( TAB );

# Done

print qq(</body>\n</html>\n);
exit ( 0 );


The "Camel Book" is essential and the Cookbook is useful. If you want to change the location of your scripts look at the part of your Apache configuration around "ScriptAlias"

ARhere
December 31st, 2011, 10:48 PM
Perfect. Need to modify to make the date more human readable but a little Google has me working on that already.

Thank you again!
-AR