PHP - 表单错误处理问题

时间:2012-12-30 11:44:21

标签: php html forms error-handling

如果不满足某些条件,我正在尝试创建错误消息。因此,用户填写表单,如果字段为空或未通过我的验证,则返回错误消息。

这是表格:

if (isset($_POST)) {
    if (checkEmail($email) == TRUE && $name != NULL && $surName != NULL) {    
        mysql_query(    "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                         VALUES ('$name', '$email','$surName') ") or die(mysql_error());
        header('Location: thanks.php');
    } 

    else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">First Name</label>
                <input type="text" name="name" id="name" value="' .$_POST['name'].'" />
                <span class="required">&#42;</span>

                <label for="surName">Last Name</label>
                <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
    }
} else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">First Name</label>
                <input type="text" name="name" id="name" value="" />
                <span class="required">&#42;</span>

                <label for="surName">Last Name</label>
                <input type="text" name="surName" id="surName" value="" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
                <span class="required">&#42;</span>


                <input type="submit" name="submit" id="submit">
            </form>';
}

所以我尝试添加一个数组来显示错误信息,如下所示:

$errorMessage = array();

并使用正确的消息将其添加到html表单字段:

$error[] = "Error Message";

现在我所困扰的是,我只想在用户不符合条件的情况下显示错误

if ($name == NULL) {$error[] = "Error Message";}
if ($surName == NULL) {$error[] = "Error Message 2";}
if (checkEmail($email) == FALSE || NULL) {$error[] = "Error Message 3";}

但我不能让它发挥作用。当我尝试实现这个逻辑时,它将解析页面正常并且验证也可以正常工作,但如果我将所需字段留空,则错误消息将不会显示。我的猜测是我没有正确地循环它。

非常感谢帮助!

编辑

我尝试了Frosty Z发布的答案,这就是我现在所拥有的:

    if (isset($_POST)) {
    $errorMessage = array();
        if ($name == '') { $errors[] = "Input name please." }
        if ($surName == '') { $errors[] = "Input last name please." }
        if (!checkEmail($email)) { $errors[] = "Email address not valid." }

    if (count($error) == 0) { 
            mysql_query(    "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                             VALUES ('$name', '$email', '$surName') ") or die(mysql_error());
            header('Location: thanks.php');
            exit;
    else {

        if (count($errors) > 0)
            echo "<p>Sorry, there are problems with the information you have provided:</p>";

        foreach($errors as $error)
            echo '<p class="error">'.$error.'</p>';

        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">Name</label>
                <input type="text" name="name" id="name" value="' .$_POST['name'].'" />
                <span class="required">&#42;</span>

                <label for="surName">Last name</label>
                <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
    }
} else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">Name</label>
                <input type="text" name="name" id="name" value="" />
                <span class="required">&#42;</span>

                <label for="surName">Achternaam</label>
                <input type="text" name="surName" id="surName" value="" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
}

这样我的页面就不会被解析了。我有错误报告但除了

之外它没有显示任何内容
  

内部服务器错误500

在我的控制台日志(Firebug)

1 个答案:

答案 0 :(得分:3)

以下是对错误消息的最小处理的一些重写。

顺便说一句,你应该考虑采用一个不错的PHP框架来帮助你处理很多常见的开发任务。

$name = '';
$surName = '';
$email = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

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

    $errors = array();

    if ($name == '') { $errors[] = "Please type your name."; }
    if ($surName == '') { $errors[] = "Please type your surname."; }
    if (!checkEmail($email)) { $errors[] = "Wrong email format."; }

    if (count($errors) == 0) {
        // tip: use PDO or mysqli functions instead of mysql ones to bind variables.
        // currently there is a risk of SQL injection here
        mysql_query("INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                     VALUES ('$name', '$email','$surName') ") or die(mysql_error());
        header('Location: thanks.php');
        exit;
    }
}

if (count($errors) > 0)
    echo '<p>Sorry, there are problems with the information you have provided:</p>';

foreach($errors as $error)
    echo '<p class="error">'.$error.'</p>';

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">            
            <label for="name">First Name</label>
            <input type="text" name="name" id="name" value="'.htmlspecialchars($name).'" />
            <span class="required">&#42;</span>

            <label for="surName">Last Name</label>
            <input type="text" name="surName" id="surName" value="'.htmlspecialchars($surName).'" />
            <span class="required">&#42;</span>

            <label for="email">E-mail</label>
            <input type="email" id="email" name="email" placeholder="example@domain.com" value="'.htmlspecialchars($email).'" />
            <span class="required">&#42;</span>

            <input type="submit" name="submit" id="submit">
        </form>';