Get input from HTML form in other document using perl and checking input

ぐ巨炮叔叔 提交于 2019-12-24 08:27:26

问题


I have two seperate perl documents:

  1. mailcheck.pl
  2. 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:

  1. 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;
    
  2. 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...)


回答1:


So I have solved question a) like this (it seems to be working):

 open(my $ml, ">", "./../logs/maillog.txt") or die "Saving Error";
 print $ml scalar(localtime()), " Email: ", $emailinput;
 close($ml);
 #print "\nYour eMail was successfully logged!", $emailinput, "\n\n";
 print "Content-type: text/html\n\n";
 print <<HTML;
 <html>
 <head>
 <title>Mailcheck2</title>
 </head>
 <body>
   <p>Es wurde die folgende eMail erfolgreich gelogged:</p>
 </body>
 </html>
 HTML
 exit; 

Now I only need help with question b).




回答2:


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;
  }
}



回答3:


Do not rely on JavaScript in this way. JS is useful an additional sanity check for your form, but it should not be the only nor the primary means of validating your data.

Instead, put all your data into a single page, and for initial development, remove any JS. Make your form a POST request, and detect when the form is posted to determine if you need to do the validation. You'll need to use your JS validation regex in your perl instead, although eventually you should upgrade to Email::Valid.

After your form and post is working without JavaScript, you can add an onSubmit method if you want to do some clientside verification to duplicate what you've done in perl.



来源:https://stackoverflow.com/questions/23747481/get-input-from-html-form-in-other-document-using-perl-and-checking-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!