使用Perl从其他文档获取来自HTML表单的输入并检查输入 [英] Get input from HTML form in other document using perl and checking input
问题描述
我有两个单独的perl文件:
- mailcheck.pl
- loggingpage.pl
Mailcheck在html中输出一个表单,用户可以在其中输入电子邮件并检查输入是否为有效的电子邮件地址。然后它调用loggingpage.pl,将电子邮件保存在日志文件中,并在电子邮件地址成功存储时向用户发送信息。
这两个文档:
$ b
-
mailcheck.pl:
#!/ usr / bin / perl
使用strict;
使用警告;
printContent-type:text / html\\\
\\\
;
print<< HTML;
< html>
< head>
< title> Mailcheck< / title>
< / head>
< body>
< form name =ecaction =./ loggingfiles.plmethod =get>
电子邮件地址:< input type =textname =email> <峰; br>
< input type =buttonvalue =Pruefenonclick =javascript:emailcheck();>
< / form>
< script language =javascripttype =text / javascript>
函数emailcheck()
{
var emailoutline = /^[a-z0-9._%+-]+\@[a-z0-9.-]+\ 。[AZ] {2,4} \ $ \ / \;
var x = document.ec.email.value;
if(emailoutline.test(x))
{
open(./ loggingpage.pl);
}
else
{
提醒(Dies ist keine richtige eMailadresse!);
}
}
< / script>
< / body>
< / html>
HTML
exit;
-
loggingpage.pl:
#!/ usr / bin / perl
使用strict;
使用警告;
使用CGI qw (:standart);
#读取名为email的表单输入
$ emailinput = param('email');
#保存电子邮件在日志文件
打开(我的$ ml,>,./../logs/maillog.txt)或死于Fehlermeldung;
打印$ ml scalar(localtime()) ,Email:,$ emailinput;
close($ ml);
#提供关于保存过程的信息
if(...)#saved email in日志文件成功
{
print<< HTML;
< html>
< head>< / head>
< body>
alert(您的输入已保存在以下电子邮件中:+ $ emailinput);
< / script>
< / body>
< / html>
HTML
exit;
}
else#如果信息没有正确存储,则发出警告
{
print<< HTML;
< html>
< head>< / head>
< body>
< script language =javascripttype =text / javascript>
alert(保存输入时出错。);
< / script>
< / body>
< / html>
HTML
exit;
}
<< HTML(..)HTML
块在它们前面不能有空格 - 在我的实际文档中,我已经以正确的方式编辑了块)
现在我的问题是:
a。当我想查看电子邮件是否保存在日志文件中时,如何编写if-conditions?
b。我不太确定部分 $ emailinput = param('email');
是否可以工作,因为我无法尝试它,是否正确地获取输入mailcheck.pl中的表单或我需要以不同的方式编写代码?
您的信息:
- mailcheck.pl正常工作。
- 在日志文件中保存电子邮件也可以正常工作(我试图通过将变量定义为电子邮件并将其与当前日期一起保存到日志文件中...)
> document.ec.submit(); //'ec'是表单的名称
这会将表单数据发送到定义的页面在你的表单的 action
属性中。 更好的是,使用 onsubmit
< form name =ecaction =code> ./loggingfiles.plmethod =getonsubmit =return emailcheck()>
然后修改验证方法,以便在失败时返回false:
函数emailcheck()
{
var emailoutline = /^[a-z0-9._%+-]+\@[a -z0-9 .-] + \ [AZ] {2,4} \ $ \ / \。
var x = document.ec.email.value;
if(!emailoutline.test(x))
{
alert(Dies ist keine richtige eMailadresse!);
返回false;
}
}
I have two seperate perl documents:
- mailcheck.pl
- loggingpage.pl
Mailcheck prints out a form in html where the user can put in her/his email and checks if the input is a valid email address. Then it calls upon the loggingpage.pl which saves the email in a logfile and gives out information to the user if the email address was successfully stored.
These are the two documents:
mailcheck.pl:
#!/usr/bin/perl use strict; use warnings; print "Content-type: text/html\n\n"; print <<HTML; <html> <head> <title>Mailcheck</title> </head> <body> <form name="ec" action ="./loggingfiles.pl" method="get"> Email: <input type="text" name="email"> <br> <input type="button" value="Pruefen" onclick="javascript:emailcheck();"> </form> <script language="javascript" type="text/javascript"> function emailcheck() { var emailoutline = /^[a-z0-9._%+-]+\@[a-z0-9.-]+\.[a-z]{2,4}\$\/\; var x = document.ec.email.value; if (emailoutline.test(x)) { open("./loggingpage.pl); } else { alert("Dies ist keine richtige eMailadresse!"); } } </script> </body> </html> HTML exit;
loggingpage.pl:
#!/usr/bin/perl use strict; use warnings; use CGI qw(:standart); #reads the input of the form called "email" $emailinput = param('email'); #saves the email in the logfile open(my $ml, ">", "./../logs/maillog.txt") or die "Fehlermeldung"; print $ml scalar(localtime()), " Email: ", $emailinput; close($ml); #gives out information about the saving process if (...) #saving the email in the logfile succeeded { print <<HTML; <html> <head></head> <body> <script language="javascript" type="text/javascript"> alert("Your input has been saved under the following email: " + $emailinput); </script> </body> </html> HTML exit; } else #gives out warning if information was not stored right { print <<HTML; <html> <head></head> <body> <script language="javascript" type="text/javascript"> alert("Error while saving your input."); </script> </body> </html> HTML exit; }
(I know that the <<HTML (..) HTML
blocks cannot have spaces in front of them - in my actual document I have edited the block the right way)
My questions are now:
a. How can I write the if-conditions when I want to see if the email got saved in the logfile?
b. I am not quite sure if the part $emailinput = param('email');
works since I was not able to try it, is it the right way to get the input of the form in mailcheck.pl or do I need to write the code differently?
For you information: - mailcheck.pl works correctly. - the saving of the email in the logfile also works fine (i tried it through defining a variable as an email and saving that into the logfile together with the current date...)
After performing the validation, submit the form using:
document.ec.submit(); // 'ec' is the name of your form
This will send the form data to the page defined in the action
attribute of your form.
Better yet, use the onsubmit
attribute of the form tag to perform the validation:
<form name="ec" action ="./loggingfiles.pl" method="get" onsubmit="return emailcheck()">
Then modify the validation method to return false on failure:
function emailcheck()
{
var emailoutline = /^[a-z0-9._%+-]+\@[a-z0-9.-]+\.[a-z]{2,4}\$\/\;
var x = document.ec.email.value;
if (!emailoutline.test(x))
{
alert("Dies ist keine richtige eMailadresse!");
return false;
}
}
这篇关于使用Perl从其他文档获取来自HTML表单的输入并检查输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!