#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    End a perl script without doing any thing and get no errors


    I have an html for where an email address is entered. after it's entered and submitted, I want a perl script to run and check it. If it's OK I want it to do something. If it's invalid i don't want any thing done. Here's the script I use:

    Code:
    #!/usr/bin/perl
    
    begin();
    use CGI;
    use Email::Valid;
    # use MIME::Lite;
    
    my $cgi = CGI->new;
    my %form_data = $cgi->Vars;
    
    if ( Email::Valid->address($form_data{email}) ) {
      print "Content-type: text/html\n\n";
      print "It's OK\n";
    }
    
    sub begin()
    {
     my $b__dir = (-d '/home/bgatto/perl'?'/home/bgatto/perl':( getpwuid($>) )[7].'/perl');
     unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux',map { $b__dir . $_ } @INC;
    }
    When I entered a valid email, I get the correct respones with no errors. However, if I enter an email like, hhdkasjhd then I just get an error.

    What's going wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    What was the error?

    What do you expect the user to get back if an invalid email is submitted? A blank page? An error message? A basket of fruit?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    I expect them to get a message from the dialog box displayed when an invalid email is entered. I have it setup to display using javescript and activates in the onclick option in the HTML form. Here's the html code:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function validation()
    {
    var x=document.forms["leadlist"]["name"].value;
    var z=document.forms["leadlist"]["email"].value;
    var atpos=z.indexOf("@");
    var dotpos=z.lastIndexOf(".");
    
    if (x=="" || x==null)
      {
      alert("A name must be entered.");
      return false;
      }
    
    if (z=="" || z==null || atpos<1 || dotpos<atpos+2 || dotpos+2>=z.length)
      {
      alert("A valid email must be entered.");
      return false;
      }
    }
    </script>
    </head>
    <body>
    <table align=center>
    <form name="leadlist" method=POST action="http://learneasymoney.com/cgi-bin/msgout-2.cgi">
    <tr>
    <td align=right>Name:</td>
    <td><input type="text" name="name"></td>
    </tr>
    <tr>
    <td align=right>Email:</td>
    <td><input type="text" name="email"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td align=center colspan=2><button type="submit" name="validbutton" style="font: normal normal bold 18pt arial; background:yellow; color:blue;" onclick="validation();">Send My Info</button></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Then it sounds like you probably want to be doing the validation in the javascript function inplace of or in addition to the validation in the perl script.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    What I'm looking to do is check an email address. If it's OK I want to send an email to that address. If it's not OK I want to display a message in some sort of dialog box. I understand that javascript cannot send emails. That is why I'm doing it in two separate scripts.

    Can someone show me the best way to do this?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    What problem do you have with my suggestion to do the validation in the javascript?

    In a CGI environment, Perl can't open a browser dialog box on the client. You need to use javascript if that's what you want the user to see.

    Otherwise, you have the Perl script output a new page, either directly or as a redirect to another page.

    Or, you could call the Perl script as an AJAX request and update a section of the current html page with a status message.

    Which of those choices do you want to use?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by FishMonger
    What problem do you have with my suggestion to do the validation in the javascript?

    In a CGI environment, Perl can't open a browser dialog box on the client. You need to use javascript if that's what you want the user to see.

    Otherwise, you have the Perl script output a new page, either directly or as a redirect to another page.

    Or, you could call the Perl script as an AJAX request and update a section of the current html page with a status message.

    Which of those choices do you want to use?
    Using the Javascript is good but I need to send an email if the validation is good, and from what I understand, you cannot send email via javascript. I'm not do ing this from my PC. This is all being done on a Linux web server (Hostgator).

    So how can I send the email?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    The javascript checks the validity of the email address and if valid, submits the form to the Perl script where more extensive validation could be implemented, if desired, and then sends the email. The Perl script would then output or redirect to a new page.

    My javascript knowledge is very limited, but the submit statement would be:
    Code:
    document.leadlist.submit();
    Check this out for javascript email validation. https://www.google.com/search?q=java...-a#about:blank

    I'm not sure if it makes any difference, but instead of using the "onclick" handler on the submit button, use the "onsubmit" handler in the opening form tag.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by FishMonger
    The javascript checks the validity of the email address and if valid, submits the form to the Perl script where more extensive validation could be implemented, if desired, and then sends the email. The Perl script would then output or redirect to a new page.

    My javascript knowledge is very limited, but the submit statement would be:
    Code:
    document.leadlist.submit();
    Check this out for javascript email validation. https://www.google.com/search?q=java...-a#about:blank

    I'm not sure if it makes any difference, but instead of using the "onclick" handler on the submit button, use the "onsubmit" handler in the opening form tag.
    This is exactly what I'm going for. As I explained, javascript cannot send emails so I'm only using it to validate and display a message box. It's the Perl that I'm using in the action of the form to send the email. The thing is, in perls, if there is an invalid email I want it to do nothing without giving me an error because I already got the message from the java script.

    So getting back to perl, how can I end the script without doing any thing?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Which takes us back to the questions I asked in my first post.

    It appears to me that you do not fully understand how cgi apps work.

    So getting back to perl, how can I end the script without doing any thing?
    You can't. The script needs to do/return something to the user, even if that means a blank page.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Code:
    if ( Email::Valid->address($form_data{email}) ) {
      print "Content-type: text/html\n\n";
      print "It's OK\n";
    }
    else {
      # do nothing, but output the http header and nothing else to prevent an error
      print "Content-type: text/html\n\n";
    }
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    781
    Rep Power
    495
    I do not do CGI programming, but I wonder what makes you think you can validate a mail address.

    I have worked on that, this is a complicated question.

    You can invalidate addresses that are oviously wrong (say there is no @ in it, no domain, etc.), but there is essenrtially no way to say that a well-formed address is right or wrong, for example that foo.bar@baz.com is wrong without at least trying to send an e-mail to this address and waiting for the answer.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by FishMonger
    Code:
    if ( Email::Valid->address($form_data{email}) ) {
      print "Content-type: text/html\n\n";
      print "It's OK\n";
    }
    else {
      # do nothing, but output the http header and nothing else to prevent an error
      print "Content-type: text/html\n\n";
    }
    Then is there a way to print in a separate window? If I use:

    Code:
    print "Content-type: text/html\n\n";
    print "email sent successfully.\n";
    it writes this on the pageI'm urrently on, clearing everything out first.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    it writes this on the pageI'm urrently on, clearing everything out first.
    No, that's not what it's doing.

    When you submit a standard cgi form, the page that you were on when you submitted is no longer there/valid. It's gone. The cgi script that processes the form will need to send back to the user a complete new page. That could be done via an http redirect, or the script could generate a completely new page.

    If you don't want to leave the form submission page, then the form needs to be submitted as an AJAX request, which can return only the "email sent successfully" message which is then used to update s section of the original page.

    AJAX Tutorial
    jQuery AJAX tutorial

IMN logo majestic logo threadwatch logo seochat tools logo