Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Need help with simple HTML form post to file

  1. #1
    Join Date
    Nov 2007
    Location
    Parker, CO
    Beans
    359
    Distro
    Ubuntu 10.04 Lucid Lynx

    Need help with simple HTML form post to file

    Hello People-who-are-smarter-than-me.

    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...
    Code:
    <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
    Code:
    echo "hello world" >> messages.txt
    Sorry but I am an electrical engineer so forgive me if this is HTML 101. How can I easily do this?
    -AR
    Actors make their living playing pretend, so why do we listen to them about how we should live in the real world?

  2. #2
    nmaster is offline Extra Foam Sugar Free Ubuntu
    Join Date
    Jun 2009
    Beans
    718

    Re: Need help with simple HTML form post to file

    do you have php installed on the web server? this covers a slightly more complicated problem, should definitely help:
    http://www.dreamincode.net/forums/to...xtarea-editor/

    PS: im an EE person too
    Last edited by nmaster; December 29th, 2011 at 07:10 AM.

  3. #3
    Join Date
    Jul 2008
    Location
    Athens, Georgia
    Beans
    228

    Re: Need help with simple HTML form post to file

    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.

  4. #4
    nmaster is offline Extra Foam Sugar Free Ubuntu
    Join Date
    Jun 2009
    Beans
    718

    Re: Need help with simple HTML form post to file

    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

  5. #5
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    perl script

    Here's a short perl script that reads a single value from HTML form data and then appends that value to a text file.

    Code:
    #!/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 );
    Last edited by Lars Noodén; December 29th, 2011 at 03:05 PM. Reason: tr/// for removing control characters if any

  6. #6
    Join Date
    Nov 2007
    Location
    Parker, CO
    Beans
    359
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Need help with simple HTML form post to file

    Thanks Everyone!

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

    -AR
    Last edited by ARhere; December 31st, 2011 at 08:36 PM. Reason: Bad Spelling
    Actors make their living playing pretend, so why do we listen to them about how we should live in the real world?

  7. #7
    Join Date
    Nov 2007
    Location
    Parker, CO
    Beans
    359
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Need help with simple HTML form post to file

    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.
    Code:
    <p><form name="input" action="perl ./getmessage.pl" method="post">
    Message: <input type="text" name="usmr" />
    <input type="submit" value="Submit" />
    </form>
    Actors make their living playing pretend, so why do we listen to them about how we should live in the real world?

  8. #8
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    /usr/lib/cgi-bin/

    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:

    Code:
    <p><form name="input" action="/cgi-bin/getmessage.pl" method="post">
    That's assuming the defaults for Apache2.

  9. #9
    Join Date
    Nov 2007
    Location
    Parker, CO
    Beans
    359
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: /usr/lib/cgi-bin/

    Quote Originally Posted by Lars Noodén View Post
    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:

    Code:
    <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.

    Thanks again Lars,
    -AR
    Actors make their living playing pretend, so why do we listen to them about how we should live in the real world?

  10. #10
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    localtime()

    Then I would add a little to the script:

    Code:
    #!/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"
    Last edited by Lars Noodén; December 31st, 2011 at 10:34 PM. Reason: fixed date error

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •