php表单验证不起作用:电子邮件发送时使用空字段


php form validation not working: email sends with empty fields

我在表单中遇到了一些困难,无法按预期工作。实际上,当我点击提交按钮时,它会发送电子邮件,即使我(认为)已经设置了验证以阻止它发送,直到所有相关字段都完成。

<?php
$page_title = "EcoPiggy: PHP Contact Us- Testing";
// define variables and set to empty values
$firstName = $lastName = $email = $telephone = $message = $marketingConsent = $copyEmail = "";
$firstNameErr = $lastNameErr = $emailErr = $telephoneErr = $messageErr = "";

if (isset($_POST['signup'])) {
    if (!empty($_POST['tracey'])) {
        die;
    }
    $marketingConsent = ($_POST["marketingConsent"]);
    $copyEmail = ($_POST["copyEmail"]);
    if (empty($_POST["firstName"])) {
        $firstNameErr = "* First Name is required";
    } else {
        $firstName = test_input($_POST["firstName"]);
        if (!preg_match("/^[a-zA-Z0-9 ]*$/", $firstName)) {
            $firstNameErr = "* Only letters and white space allowed";
        }
        $min = 3;
        if (strlen($firstName) < $min) {
            $firstNameErr = "Validation failed: Too Small minimum 3 characters";
        }
        $max = 45;
        if (strlen($firstName) > $max) {
            $firstNameErr = "Validation failed: Too Large maximum  45 characters";
        }
    }
    if (empty($_POST["lastName"])) {
        $lastNameErr = "* Last Name is required";
    } else {
        $lastName = test_input($_POST["lastName"]);
        if (!preg_match("/^[a-zA-Z0-9 ]*$/", $lastName)) {
            $lastNameErr = "* Only letters and white space allowed";
        }
        $min = 3;
        if (strlen($lastName) < $min) {
            $lastNameErr = "Validation failed: Too Small minimum 3 characters";
        }
        $max = 45;
        if (strlen($lastName) > $max) {
            $lastNameErr = "Validation failed: Too Large maximum  45 characters";
        }
    }
    if (empty($_POST["email"])) {
        $emailErr = "* email address is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "* Invalid email format";
        }
        $min = 6;
        if (strlen($email) < $min) {
            $emailErr = "Validation failed: Too Small minimum 6 characters";
        }
        $max = 60;
        if (strlen($email) > $max) {
            $emailErr = "Validation failed: Too Large maximum  60 characters";
        }
    }
    if (empty($_POST["telephone"])) {
        $telephoneErr = "* Please enter your telephone number";
    } else {
        $telephone = test_input($_POST["telephone"]);
        if (!preg_match("/^[1-9][0-9]{6-13}*$/", $telephone)) {
            $telephoneErr = "* Only numbers and white space allowed";
        }
        $min = 6;
        if (strlen($telephone) < $min) {
            $lastNameErr = "Validation failed: Too Small minimum 6 characters";
        }
        $max = 13;
        if (strlen($telephone) > $max) {
            $telephoneErr = "Validation failed: Too Large maximum  13 characters";
        }
    }
    if (empty($_POST["message"])) {
        $messageErr = "* Your message is required";
    } else {
        $message = test_input($_POST["message"]);
        $min = 3;
        if (strlen($message) < $min) {
            $messageErr = "Validation failed: Too Small minimum 3 characters";
        }
        $max = 1000;
        if (strlen($message) > $max) {
            $messageErr = "Validation failed: Too Large maximum  45 characters";
        }
    }
    if ($marketingConsent == 0) {
        $marketingConsent = "Thank you for trusting us to contact periodically with 3rd party promotions";
    } else {
        $marketingConsent = "I do not want the information to be used by anybody for direct marketing purposes";
    }
    $create_email = '<ul>';
    $create_email .='<li>First Name: ' . $firstName . '</li>';
    $create_email .='<li>Last Name: ' . $lastName . '</li>';
    $create_email .='<li>Telephone Number: ' . $telephone . '</li>';
    $create_email .='<li>Email address: ' . $email . '</li>';
    $create_email .='<li>Your message: ' . $message . '</li>';
    $create_email .='<li>Marketing consent: ' . $marketingConsent . '</li>';
    $create_email .='<li>Cc: ' . $copyEmail . '</li>';
    $create_email .= '</ul>';
    $header1 = "From: webform@ecopiggy.co.uk 'r'n";
    $header1 .= "Reply-To: {$email} 'r'n";
    if ($copyEmail == 1) {
        $header1 .= "Cc: {$email}'r'n";
    } else {
        $header1 .= "";
    }
    $header1 .= "MIME-Version: 1.0" . "'r'n";
    $header1 .= "Content-Type: text/html; charset=ISO-8859-1";

    $to = "hello@ecopiggy.co.uk";
    $subject = 'Ecopiggy - Contact-Us' . strftime("%T", time());
    $message = $create_email;
    $headers = $header1;
    $result = mail($to, $subject, $message, $headers);
    if (isset($result)) {
        redirect_to("thankyou.php");
    } else {
        redirect_to("contact-us.php");
    }
}
?>

有人能在验证出错的地方提供帮助吗?

非常感谢,

阿萨。

问题是,即使您正在验证表单输入,在构建邮件之前也没有检查是否存在任何错误,即$firstNameErr$lastNameErr等是否为空。

if(empty($firstNameErr) && empty($lastNameErr) && empty($emailErr) && empty($telephoneErr) && empty($messageErr)){
    // construct the mail and send it
}else{
    // display error messages
}

所以你现有的代码应该是这样的:

<?php
    $page_title = "EcoPiggy: PHP Contact Us- Testing";
    // define variables and set to empty values
    $firstName = $lastName = $email = $telephone = $message = $marketingConsent = $copyEmail = "";
    $firstNameErr = $lastNameErr = $emailErr = $telephoneErr = $messageErr = "";    
    if (isset($_POST['signup'])) {
        if (!empty($_POST['tracey'])) {
            die;
        }
        $marketingConsent = ($_POST["marketingConsent"]);
        $copyEmail = ($_POST["copyEmail"]);
        if(empty($_POST["firstName"])) {
            $firstNameErr = "* First Name is required"; 
        } else {
            $firstName = test_input($_POST["firstName"]);
            if (!preg_match("/^[a-zA-Z0-9 ]*$/",$firstName)) {
                $firstNameErr = "* Only letters and white space allowed"; 
            } 
            $min=3;
            if(strlen($firstName) < $min) {
                $firstNameErr = "Validation failed: Too Small minimum 3 characters"; 
            }
            $max=45;
            if(strlen($firstName) > $max) {
                $firstNameErr = "Validation failed: Too Large maximum  45 characters";  
            }
        }
        if(empty($_POST["lastName"])) {
            $lastNameErr = "* Last Name is required";
        } else {
            $lastName = test_input($_POST["lastName"]);
            if (!preg_match("/^[a-zA-Z0-9 ]*$/",$lastName)) {
                $lastNameErr = "* Only letters and white space allowed"; 
            }
            $min=3;
            if(strlen($lastName) < $min) {
                $lastNameErr = "Validation failed: Too Small minimum 3 characters"; 
            }
            $max=45;
            if(strlen($lastName) > $max) {
                $lastNameErr = "Validation failed: Too Large maximum  45 characters";   
            }
        }   
        if(empty($_POST["email"])) {
            $emailErr = "* email address is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $emailErr = "* Invalid email format";
                echo $emailErr;
            }
            $min=6;
            if(strlen($email) < $min) {
                $emailErr = "Validation failed: Too Small minimum 6 characters";    
            }
            $max=60;
            if(strlen($email) > $max) {
                $emailErr = "Validation failed: Too Large maximum  60 characters";  
            }
        }
        if(empty($_POST["telephone"])) {
            $telephoneErr = "* Please enter your telephone number";
        } else {
            $telephone = test_input($_POST["telephone"]);
            if (!preg_match("/^[1-9][0-9]{6-13}*$/",$telephone)) {
                $telephoneErr = "* Only numbers and white space allowed"; 
            }
            $min=6;
            if(strlen($telephone) < $min) {
                $lastNameErr = "Validation failed: Too Small minimum 6 characters"; 
            }
            $max=13;
            if(strlen($telephone) > $max) {
                $telephoneErr = "Validation failed: Too Large maximum  13 characters";  
            }   
        }
        if(empty($_POST["message"])) {
            $messageErr = "* Your message is required";
        } else {
            $message = test_input($_POST["message"]);
            $min=3;
            if(strlen($message) < $min) {
                $messageErr = "Validation failed: Too Small minimum 3 characters";  
            }
            $max=1000;
            if(strlen($message) > $max) {
                $messageErr = "Validation failed: Too Large maximum  45 characters";    
            }
        }
        if($marketingConsent == 0) {
            $marketingConsent = "Thank you for trusting us to contact periodically with 3rd party promotions";
        } else {
            $marketingConsent = "I do not want the information to be used by anybody for direct marketing purposes";
        }
        if(empty($firstNameErr) && empty($lastNameErr) && empty($emailErr) && empty($telephoneErr) && empty($messageErr)){
            $create_email = '<ul>';
            $create_email .='<li>First Name: '.$firstName.'</li>';  
            $create_email .='<li>Last Name: '.$lastName.'</li>';
            $create_email .='<li>Telephone Number: '.$telephone.'</li>';
            $create_email .='<li>Email address: '.$email.'</li>';
            $create_email .='<li>Your message: '.$message.'</li>';
            $create_email .='<li>Marketing consent: '.$marketingConsent.'</li>';
            $create_email .='<li>Cc: '.$copyEmail.'</li>';
            $create_email .= '</ul>';
            echo $create_email;
            $header1 = "From: webform@ecopiggy.co.uk 'r'n";
            $header1 .= "Reply-To: {$email} 'r'n";
              if ($copyEmail == 1) {
                $header1 .= "Cc: {$email}'r'n";
                } else {
                $header1 .= "";
            }
            $header1 .= "MIME-Version: 1.0" ."'r'n";
            $header1 .= "Content-Type: text/html; charset=ISO-8859-1";

            $to = "hello@ecopiggy.co.uk";
            $subject = 'Ecopiggy - Contact-Us' .strftime("%T", time());
            $message = $create_email;
            $headers = $header1;
            $result = mail($to, $subject, $message, $headers);
            if  (isset($result)) {
                redirect_to("thankyou.php");
            } else {
                redirect_to("contact-us.php");
            }
        }else{
            // display errors
        }
    }
?>

您正在进行浏览器端验证,因此javascript是最好的。http://www.w3schools.com/js/js_validation.asp这是您可以在其中找到验证的链接。

当你提到你正在使用HYML5时,只需在标签中添加"required",这意味着该字段是强制性的。HTML5

中还有其他预定义的验证

脚本清除所有变量,然后检查$_POST变量。然而,当检查通过时,您不会设置新值——除非有一个您没有在上面显示的test_input函数:

$telephone = test_input($_POST["telephone"]);

所以在页面顶部设置

$telephone = '';

然后检查

$_POST['telephone'];

是有效的,如果是,则将$phone设置为无法解析的值。然后在您的电子邮件中,您尝试发送$phone,但该值仍然为空。