我的php表单没有检查我的表单中输入的电子邮件是否有效

时间:2014-01-29 19:44:43

标签: php forms validation error-handling

我需要一些帮助:

  1. 我有一个名为isValid的函数,它不检查在我的表单中输入的电子邮件是否有效?

  2. 如何在表单的文本字段中显示错误消息?

  3. 非常感谢任何帮助!

    以下是我的代码:

     <?php 
    
         //Set Variables to Empty String
         $Email     = " ";
         $Subject   = " ";
         $Name      = " ";
         $Message   = " ";
    
         $error    = " ";
    
    
         if(isset($_POST['submit']) )
         {
    
               if (empty($_POST["Email"]))
               {
                  $error = "** Enter a valid email";
               }
               else
               {
                  $Email = isValid($_POST["Email"]);
               }
               if (empty($_POST["Subject"]))
               {
                  $error = "** Enter a subject";
               }
               else
               {
                  $Subject= test_input($_POST["Subject"]);
               }
               if (empty($_POST["Name"]))
               {
                  $error = "** Enter your name";
               }
               else
               {
                  $Name= test_input($_POST["Name"]);
               }
               if (empty($_POST["Message"]))
               {
                  $error = "** Enter your message";
               }
               else
               {
                  $Message= test_input($_POST["Message"]);
               }
          }
    
    
    function test_input($data)
    {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    
    //isValid checks if email address is a valid one
    function isValid($edata)
    {   
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]* [[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][ 0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $edata));
    }
    
    ?>
    
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    
    <p><label>From (Email):</label></p>
    <input type="text" size="35" name="Email">
    <span class="error"><?php echo $error;?></span>
    
    <br><br>
    
    <p><label>Subject:</label></p>
    <input type="text" size="35" name="Subject">
    <span class="error"><?php echo $error;?></span>
    
    <br><br>
    
    <p><label>Name:</label></p>
    <input type="text" size="35" name="Name">
    <span class="error"><?php echo $error;?></span>
    
    <br><br>
    
    <p><label>Message:</label></p>
    <textarea type="text" cols="38" rows="6" name="Message"></textarea>
    <span class="error"><?php echo $error;?></span>
    
    <br><br>
    
    <input type="submit" name="submit" value="submit">
    <input type="reset" value="Reset">
    
    </form>
    <?php
    
    if(empty($error))
    {
    // the email will be sent here
    $to      = "@gmail.com";
    
    // the email subject
    $subject = 'Message from XXXX website from: ' . $Name;
    
    // the mail message
    
    $msg    .= "\r\nEmail:     $Email";
    $msg    .= "\r\n\nSubject: $Subject";
    $msg    .= "\r\n\nName:    $Name";
    $msg    .= "\r\n\nMessage: $Message";
    
    
    mail($to, $subject, $msg, "From: $Email\r\nReply-To: $Email\r\nReturn-Path: $Email\r\n");
    }
    
    ?>
    
    <p>Thank you <b><?=$Name;?></b> for your message. Expect a response in 1 - 3 business   days</p>    
    

2 个答案:

答案 0 :(得分:0)

validate functions

<?php
$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.";
}
?>

答案 1 :(得分:0)

第二个问题,改变你的代码:

       if (empty($_POST["Email"]))
       {
          $error['Email'] = "** Enter a valid email";
       }
       .
       .
       .
       if (empty($_POST["Name"]))
       {
          $error['Name'] = "** Enter your name";
       }
.
.
.

<input type="text" size="35" name="Email">
<span class="error"><?php echo $error['Email'];?></span>

<input type="text" size="35" name="Name">
<span class="error"><?php echo $error['Name'];?></span>