1

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...)

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Alright thank you - and how to I read the input from the form in loggingpage.pl?
0

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.

1 Comment

Thank you for the post, however, I am supposed to use this much JS - it is an homework assignement and it specifies the use of JS and regular expressions in mailcheck.pl. Any ideas on how to solve my questions even though I have to use JS?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.