PHP电子邮件验证不起作用

时间:2014-11-21 19:06:52

标签: php validation email

我有一个表单,而不是我提交错误时没有显示错误

    <form action="emailSubs.php" method="post">
        <p>Would you like to subscribe to our newsletter ?</p>
        <p>Name: <input type="text" name="name"><br /></p>
        <p>E-mail: <input type="text" name="Email"><br /></p>
        <p><input type="submit" name="submit"><br /></p>
    </form>
    <?php
    function validateEmail($data, $fieldName) {
        global $errorCount;
        if(empty($data)) {
            echo "\"$fieldName\" is a required
            field.<br />\n";
            ++$errorCount;
            $retval = "";
        } else { // olny clean up the input if it isn't
                // empty
        $retval = trim($data);
        $retval = stripslashes($retval);
        $pattern = "/^[\w-]+(\.[\w-]+)*@" .
             "[\w-]+(\.[\w-]+)*" .
             "(\[[a-z]]{2,})$/i";
    if(preg_match($pattern, $retval) ==0) {
        echo "\"$fieldName\" is not a valid E-mail
        address.<br />\n";
        ++$errorCount;
        }
    }
    return ($retval);
}
    ?>

我认为这可能是模式,但不确定问题可能是什么

2 个答案:

答案 0 :(得分:0)

问题是你没有正确连接这两件事......

将表单保留在emailSubs.php的单独文件中 - 虽然这不是必要的步骤,但它有望帮助您了解其工作方式(更不用说它是一种更整洁/有组织的方式)

   <form action="emailSubs.php" method="post">
        <p>Would you like to subscribe to our newsletter ?</p>
        <p>Name: <input type="text" name="name"><br /></p>
        <p>E-mail: <input type="text" name="Email"><br /></p>
        <p><input type="submit" name="submit"><br /></p>
    </form>

现在,在emailSubs.php文件中:

<?php
function validateEmail($data, $fieldName) {
    global $errorCount;
    if(empty($data)) {
        echo "\"$fieldName\" is a required
        field.<br />\n";
        ++$errorCount;
        $retval = "";
    } else { // olny clean up the input if it isn't
            // empty
    $retval = trim($data);
    $retval = stripslashes($retval);
    $pattern = "/^[\w-]+(\.[\w-]+)*@" .
         "[\w-]+(\.[\w-]+)*" .
         "(\[[a-z]]{2,})$/i";
if(preg_match($pattern, $retval) ==0) {
    echo "\"$fieldName\" is not a valid E-mail
    address.<br />\n";
    ++$errorCount;
    }
}
return ($retval);

}     ?&GT;

但是,你还没有完成 - ! 你看,你必须连接两个--- 在您的表单中,您指定了method="post" - 所以,我们这样做:

    <?php

$name = $_POST['name'];
$email = $_POST['email']; 

?> 

现在,有很多充分的理由不使用regexp来验证您的表单。 This is a good read on that topic.

所以,你可能会做的事情看起来像这样:

<?php

if(ctype_alnum($_POST['name']) == true){
$name = $_POST['name'];

} else {

exit("Please enter a valid name");

}

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL){
    $email = $_POST['email']; 
} else {

    exit("Please enter a valid email address");

}

?> 

你看到了吗?这使得处理验证的方式更加清晰。

所以,圆圈,你的代码没有显示错误,因为没有任何东西可以显示该错误。

答案 1 :(得分:0)

我注意到你有一个表单和一个函数,但是在提交表单时你没有调用该函数。也许这是您在所包含的代码范围之外所做的事情,但为了以防万一,我将其修改为提交/函数调用与表单本身之间的完整交互。另外,为什么不使用filter_var而不是正则表达式?

代码(在我的本地服务器上工作):

<?php
function validateEmail($data, $fieldName)
{
    global $errorCount;
    $errorCount=0;
    if(empty($data))
    {
        echo "\"$fieldName\" is a required
        field.<br />\n";
        ++$errorCount;
        $retval = "";
    } 
    else 
    { 
        // olny clean up the input if it isn't
        // empty
        $retval = trim($data);
        $retval = stripslashes($retval);

        if(!filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL))
        {
            echo "\"".$_POST['Email']."\" is not a valid E-mail
            address.<br />\n";
            ++$errorCount;
        }
    }
    return ($retval);
}
if(isset($_POST['submit']))
{
    $email=validateEmail($_POST['Email'], "Email");
    if(empty($errorCount))
    {
        //create subscription
        echo "Subscribed!";
    }
}
?>   
<form action="test.php" method="post">
    <p>Would you like to subscribe to our newsletter ?</p>
    <p>Name: <input type="text" name="name" value="<?php echo $_POST['name'];?>"><br /></p>
    <p>E-mail: <input type="text" name="Email" value="<?php echo $_POST['Email'];?>"><br /></p>
    <p><input type="submit" name="submit"><br /></p>
</form>