3

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

这是表格:

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

在我的控制台日志中(萤火虫)

4

1 回答 1

4

这是对您的工作的一些重写,对错误消息的处理最少。

顺便说一句,您应该考虑采用一个体面的 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>';
于 2012-12-30T11:59:41.043 回答