PHP表单验证代码用于工作正常,停止工作,我不知道为什么

时间:2012-02-13 19:26:55

标签: php validation

我的网站上有一些非常基本的PHP验证代码,它已经工作了很长时间。我不是任何方式的PHP专家(显然下面我确定):),但这个代码到目前为止我的需求工作正常...我最近意识到这个代码已经停止工作,我不知道为什么。任何帮助,将不胜感激。我想也许在服务器上有所改变,所以我和我的主机公司聊天,他们向我保证没有任何改变,并且它必须是代码错误。

以下是代码的基本部分:

<?php // Handle the form submission

    // VALIDATE Forms Required Fileds       
    //Check for name.       
    if (strlen($_POST['name']) > 0 ){
        $name = $name;
    } else {
        $name = NULL;
    }   

    //Check email was entered
    if (strlen($_POST['email'])=='') {
        $email = NULL;
    } elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
        $email = "invalid";
    } else {
        $email = $email;
    }
    // END VALIDATION

    // If all is ok then handle form
    if ((isset($_POST['Submit']) || isset($_POST['Submit_x'])) && ($name && $email && $email != "invalid"))
    {       
        // Capture information and format for email
        $subject="Side Nav Form"; // Subject of Email

        $body = "Contact Us:\n\n";      
        $body .="Name..............: " ."$name\n";
        $body .="Email.............: " ."$email\n";
        $body .="Phone.............: " ."$phone\n\n";
        $body .="Reason for inquiry: " ."$inquiryReason1, $inquiryReason2, $inquiryReason3\n";
        $body .="Current Website...: " ."$current_site\n\n";
        $body .="Comments/Questions: " ."$addtlComments\n";

        //Email form results
        mail("xxx@xxx.com", $subject, $body, "From:" .$email);

        // Display thank you Message if form submitted successfully.
        echo '<h3>Thank you!</h3><p>We received your information and will get back to you as soon as possible.</p>';

    } else {    

        // If Validating form: Show validation errors.
        if ((isset($_POST['Submit']) || isset($_POST['Submit_x'])) && (!$name || !$email || $email == "invalid")) { 
        echo '<p class="error">';
             if (!$name) {
                    echo "*Please enter your name.<br />";
                    }
             if (!$email) {
                    echo "*Please enter your email address.<br />";
                    } 
             if ($email == "invalid") {
                    echo "*Please enter a valid email address. (xxx@xxx.xxx)";
                    } 
        echo '</p>';
        }
    // Display the form if user has not submitted successfully or at all.
?>
<fieldset><label for="name">*Name:</label> <input type="text" name="name" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></fieldset>
<fieldset><label for="email">*Email:</label> <input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></fieldset>

... rest of form fields...

<input name="Submit" type="submit" value="Submit" class="submit" />
</form>
<?php       
    } // End of Form Handler
?> 

1 个答案:

答案 0 :(得分:1)

eregi()已弃用。请改用preg_match()。您也可以使用filter_var(email, FILTER_VALIDATE_EMAIL)来验证电子邮件。