php表单验证无效:电子邮件发送空字段

时间:2016-01-07 11:30:10

标签: php html5

我的形式有些困难没有按预期工作。有效地,当我点击提交按钮时,它会发送电子邮件,即使我(思考)已设置验证以阻止它发送,直到完成所有相关字段。

<?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");
    }
}
?>

任何人都可以协助验证出错的地方吗?

非常感谢,

阿萨。

3 个答案:

答案 0 :(得分:0)

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

并且正如您所提到的那样,您正在使用HYML5,因此只需在标记中添加“ required ”,这意味着此字段是必填字段。还有其他预定义验证可用在HTML5中

答案 1 :(得分:0)

您的脚本清除所有变量,然后检查$ _POST变量。但是,当检查通过时,您没有设置新值 - 除非上面没有显示test_input函数:

$mdDateLocaleProvider.formatDate = function(date) {
    return moment(date).format('DD/MM/YYYY');
};

$mdDateLocaleProvider.parseDate = function(dateString) {
    var m = moment(dateString, 'DD/MM/YYYY', true);
    return m.isValid() ? m.toDate() : new Date(NaN);
};

因此,您可以在页面顶部设置

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

然后检查是否

$telephone = '';

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

答案 2 :(得分:0)

问题是,即使您正在验证表单输入,也不会在构造邮件之前检查是否存在任何错误,即$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

        }

    }
?>