0

I have a form with many fields. Let's simplify and pretend it's only got forename/surname/email here for now. I have to use Perl to process the form because I need to do other stuff with it later.

On submitting the form I need it to do three things:

  1. To put the responses to this form into a text file on the server.

  2. To send an email alert out saying that there's been a new form submitted. It doesn't need to contain the form data, just that a new one is there.

  3. To display a “thanks for filling in the form” page to the person who's just hit submit.

I've tried concentrating on getting it to do any one of those things, but I still don't understand Perl enough to be able to do it. I'm a HTML user at best. It seems like a series of fairly simple things to do, and seems like the sort of thing for which there would be a "stock answer" somewhere, but a lot of Googling and reading of answers here hasn't given me anything! If I could just get some idea on how to do the first one, that would be a great start, but I can't even get that far… ☹</p>

4

1 回答 1

1
  1. 安装PlackMIME::Lite

    cpan Plack
    cpan MIME::Lite
    
  2. 使用纯 HTML 来构建您的表单(命名这个form.html或其他名称)。

    <form action="/send">
        <label>Enter some stuff:</label>
        <input type="text" name="stuff">
        <button type="submit">Send</button>
    </form>
    
  3. 编写一个 PSGI 应用程序(命名这个文件app.psgi)。

    use strict;
    use warnings;
    use autodie;
    use Plack::App::File;
    use Plack::Builder;
    use Plack::Request;
    use MIME::Lite;
    
    builder {
        mount '/form.html' => Plack::App::File->new( file => "form.html" );
        mount '/send' => sub {
            my $req = Plack::Request->new($env);
    
            open my $fh, '>', 'form.txt';
            print $fh $req->content; # this will be ugly, but you didn't say what format
            close $fh;
    
            my $email = MIME::Lite->new(
                From => 'website@example.com',
                To => 'user@example.com',
                Subject => 'Form submitted from web site',
                Data => 'Read the subject.',
            );
            $email->send;
    
            return [ 
                200, 
                [ 'Content-Type' => 'text/html' ], 
                [ '<h1>Thanks for filling in the form.</h1>' ], 
            ];
        };
    
  4. 运行您的网络应用程序:

    plackup --port 5000 app.psgi
    
  5. 将您的浏览器指向:http://localhost:5000

  6. 完毕。

这不是做这一切的最佳方式,但它是一种非常简单的方式来展示它是多么容易上手,并提供了一个基础。

于 2012-08-15T19:46:38.910 回答