我有一个包含许多字段的表单。让我们简化并假装它现在只有名字/姓氏/电子邮件。我必须使用 Perl 来处理表单,因为我稍后需要用它做其他事情。
提交表格时,我需要它做三件事:
-
将对此表单的响应放入服务器上的文本文件中。
-
发送电子邮件警报,说明已提交新表单。它不需要包含表单数据,只需要有一个新的数据。
-
向刚刚点击提交的人显示"感谢您填写表单"页面。
我试过专注于让它做任何一件事情,但我仍然不够了解 Perl 来做到这一点。我充其量是一个 HTML 用户。这似乎是一系列相当简单的事情要做,似乎在某处会有"股票答案"的那种事情,但是这里的很多谷歌搜索和阅读答案并没有给我任何东西!如果我能对如何做第一个有一些想法,那将是一个很好的开始,但我什至不能走那么远......☹
-
安装 Plack 和 MIME::Lite。
cpan Plack cpan MIME::Lite
-
使用纯HTML来构建表单(命名此
form.html
或其他名称)。<form action="/send"> <label>Enter some stuff:</label> <input type="text" name="stuff"> <button type="submit">Send</button> </form>
-
编写一个 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>' ], ]; };
-
运行您的 Web 应用程序:
plackup --port 5000 app.psgi
-
将浏览器指向:
http://localhost:5000
-
做。
这不是做这些事情的最佳方法,但它是展示入门是多么容易并提供构建基础的非常简单的方法。