如何在php检查表单上显示所有空输入的列表

时间:2015-02-23 18:38:17

标签: php html forms

我正在对提交的表单进行PHP检查,我想向用户显示他们忘记填写的所有字段。我也在使用jQuery,但是用户可以禁用它,而且你真的需要对表单进行服务器端检查。

问题是,如果有6个必填字段,并且他们提交表格,其中2个为空,则代码仅显示第一个,并且在再次提交之后,它将向他们显示第二个。你有什么建议去做那些事情?

以下是代码:

if (isset($_POST['submit'])){

        $message = "";

        if (trim($_POST['ign'])){
            if (trim($_POST['god'])){
                if (trim($_POST['replay_id'])){
                    if (trim($_POST['map_type'])){
                        if (trim($_POST['time_min']) AND trim($_POST['time_sec'])){
                            if (trim($_POST['description'])){
                                // Submit the form
                            }else{
                                $message .= "<li>Description is empty</li>";
                            }
                        }else{
                            $message .= "<li>Time not specified</li>";
                        }
                    }else{
                        $message .= "<li>Match type not specified.</li>";
                    }
                }else{
                    $message .= "<li>Replay ID not specified.</li>";
                }
            }else{
                $message .= "<li>God was not specified.</li>";
            }
        }else{
            $message .= "<li>In game name was not specified!</li>";
        }


        if (!empty($message)){
            $message = "<div style='text-align:left; display: inline-block;'><ul>".$message."</ul></div>";
        }else{
            $message = "Submit succesfull";
        }

        echo "<div id='close-message'><div class='admin-message'>$message</div></div>";
    }

唯一的另一种方式我可以想到这段代码:

if (isset($_POST['submit'])){

    $message = "";
    $pass = TRUE;

    if (!trim($_POST['ign'])){          $message .= "<li>In game name was not specified!</li>"; $pass = FALSE;  }
    if (!trim($_POST['god'])){          $message .= "<li>God was not specified.</li>";          $pass = FALSE;  }
    if (!trim($_POST['replay_id'])){    $message .= "<li>Replay ID not specified.</li>";        $pass = FALSE;  }
    if (!trim($_POST['map_type'])){     $message .= "<li>Match type not specified.</li>";       $pass = FALSE;  }
    if (!trim($_POST['description'])){  $message .= "<li>Description is empty</li>";            $pass = FALSE;  }
    if (!trim($_POST['time_min']) AND trim($_POST['time_sec'])){    $message .= "<li>Time not specified</li>";  $pass = FALSE;  }

    if ($pass){
        $message = "Submit succesfull";
        // Submit the form
    }else{
        $message = "<div style='text-align:left; display: inline-block;'><ul>".$message."</ul></div>";
    }

    echo "<div id='close-message'><div class='admin-message'>$message</div></div>";
}

还有其他办法吗?同样,纯粹是PHP,jQuery就在那里,但它可以被禁用,HTML5也无法在所有浏览器中使用。

谢谢。

2 个答案:

答案 0 :(得分:1)

我更喜欢将错误消息放在导致错误的字段旁边,并根据数组运行整个事件以保持代码DRY:

if (isset($_POST['submit'])){

    $fields = array(
        'ign' => 'In game name was not specified!',
        'god' => 'God was not specified.',
        ...
    );
    $message = "";
    $error = array();
    $pass = TRUE;

    foreach ($fields as $fld => $errmsg) {
        if (!trim($_POST[$fld])) {
            $message .= "<li>$errmsg</li>"; 
            $error[$fld] = $errmsg; 
            $pass = FALSE; 
        }
    }

我将$message变量留在那里,因为您的用户可能很高兴在表单顶部和任何错误字段旁边收到消息。通常我会说“你的表格提交有错误 - 见下文”。

然后在表单显示代码中向下显示$error[$fld]的值。 (我假设你再次显示表单,让用户修复他们第一次没有填写的字段。)

所以在您的表单看起来像这样之前(如果您使用的是表格):

<table>
  <tr>
    <td align="right">In Game Name:</td>
    <td><input name='ign' type='text' /></td>
  </tr>
  ...

现在看起来像这样:

<table>
  <tr>
    <td align="right">In Game Name:</td>
    <td><input name='ign' type='text' /></td>
    <td><?= @$error['ign'] ?></td>
  </tr>
  ...

通过一点点工作,您可以使用颜色为您的字段标签设置样式以吸引用户的注意力等。

当然如果您的表单很简单,那么您可以使用另一个数组(或上面的$ fields数组的扩展名),这将在一个简单的循环中再次为您完成所有这些:

$fields = array(
    'ign' => array('label' => 'In Game Name:', 'error' => 'In game name was not specified!'),
    'god' => array('label' => 'God Mode:', 'error' => 'God was not specified.'),
    ...
);

答案 1 :(得分:1)

如果您追求的是摆脱所有if语句并使您的代码更清洁,这可能是一个可能的解决方案:

设置一个包含必填字段及其相应的&#34; required&#34;的数组。错误消息。遍历数组并将字段与$_POST匹配,以找到未设置的任何必填字段,将任何此类字段的错误消息添加到$errors数组。

发现的任何错误都将显示为无序列表。如果没有错误,将显示成功消息。

// Set some values for the example
$_POST['submit'] = '1';
$_POST['ign'] = 'foo';
$_POST['god'] = 'bar';
$_POST['description'] = 'baz';

// Validate required fields if submitted 
if (isset($_POST['submit'])) {
    $required = array(
        'ign'         => 'In game name was not specified!',
        'god'         => 'God was not specified',
        'replay_id'   => 'Replay ID not specified',
        'map_type'    => 'Match type not specified',
        'description' => 'Description is empty',
        'time_min'    => 'Time not specified',
        'time_sec'    => 'Time not specified',
    );
    $errors = array();

    foreach ($required as $field => $errorMessage) {
        if (isset($_POST[$field]) && trim($_POST[$field]) != '') {
            continue; // All is well, check next required field
        }

        // No value was set for this required field
        $errors[] = $errorMessage;
    }

    if ($errors) {
        // Show any errors (use array_unique() to avoid duplicate error messages
        // on time_min/time_sec fields)
        $message = ""
            . "<div style='text-align:left; display: inline-block;'>"
            .     "<ul>"
            .         "<li>"
            .             implode('</li><li>', array_unique($errors))
            .         "</li>"
            .     "</ul>"
            . "</div>";
    }
    else {
        // All is well
        $message = "Submit successful";
    }

    echo ""
        . "<div id='close-message'>"
        .     "<div class='admin-message'>$message</div>"
        . "</div>";
}

输出(缩进以提高可读性):

<div id='close-message'>
    <div class='admin-message'>
        <div style='text-align:left; display: inline-block;'>
            <ul>
                <li>Replay ID not specified</li>
                <li>Match type not specified</li>
                <li>Time not specified</li>
            </ul>
        </div>
    </div>
</div>