表单验证 - PHP中的输入字段

时间:2012-02-16 20:25:45

标签: php arrays input foreach validation

我有一个名为'test.php'的脚本,它有 3“组的输入字段

如果符合以下条件,我需要设置 $ errors

  • 只有部分,并非全部集合中的字段已完成(因此,如果用户只完成“长度”并将高度/宽度/重量留空)
  • 如果 的字段不是数字

如果它们只包含占位符文本(高度/重量/宽度/长度),我还需要我的代码忽略一组字段。

如何修改我的附加代码以实现此目的?

<?php

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

    $errors = array();
    $all = '';

    // Loop over the values 1 through 3
    foreach( range( 1, 3) as $i)
    {
        // Create an array that stores all of the values for the current number
        $values = array( 
            'a' . $i . 'height' => $_POST['a' . $i . 'height'], 
            'a' . $i . 'width' => $_POST['a' . $i . 'width'], 
            'a' . $i . 'length' => $_POST['a' . $i . 'length'], 
            'a' . $i . 'weight' => $_POST['a' . $i . 'weight']
        );

        // Make sure at least one submitted value is valid, if not, skip these entirely
        if( count( array_filter( array_map( 'is_numeric', $values))))
        {
             // This basically checks if there's at least one numeric entry for the current $i
             continue; // Skip
        }

        // Validate every value
        foreach( $values as $key => $value)
        {
            if( empty( $value))
            {
                $errors[] = "Value $key is not set";
            }
            // You can add more validation in here, such as:
            if( !is_numeric( $value))
            {
                $errors[] = "Value $key contains an invalid value '$value'";
            }
        }

        // Join all of the values together to produce the desired output
        $all .= implode( '|', $values) . "\n";
    }



    // If there are errors
    if( count( $errors) > 0)
    {
        echo '<div class="errors">';

        // If there is only one, just print the only message
        if( count( $errors) == 1)
        {
            echo $errors[0];
        }
        // Otherwise format them into an unordered list
        else
        {
            echo '<ul>';
            echo '<li>' . implode( '</li><li>', $errors) . '</li>';
            echo '</ul>';
        }

        echo '</div>';
    }

} // end if

?>

<form action="test.php" method="POST">

<input id="a1height" name="a1height" value="<?php if (isset($_POST['submitform'])) { echo $_POST['a1height']; } ?>" />
<input id="a1width" name="a1width" value="<?php if (isset($_POST['submitform'])) { echo $_POST['a1width']; } ?>" />
<input id="a1length" name="a1length" value="<?php if (isset($_POST['submitform'])) { echo $_POST['a1length']; } ?>" />
<input id="a1weight" name="a1weight" value="<?php if (isset($_POST['submitform'])) { echo $_POST['a1weight']; } ?>" />

<br /><br />

<input id="a2height" name="a2height" value="Height" />
<input id="a2width" name="a2width" value="Width" />
<input id="a2length" name="a2length" value="Length" />
<input id="a2weight" name="a2weight" value="Weight" />
<br /><br />

<input id="a3height" name="a3height" value="Height" />
<input id="a3width" name="a3width" value="Width" />
<input id="a3length" name="a3length" value="Length" />
<input id="a3weight" name="a3weight" value="Weight" />

<input type="submit" id="submitform" name="submitform" value="submit" />

</form>

非常感谢。

0 个答案:

没有答案