表单验证-PHP中的输入字段
我有一个名为“test.php”的脚本,它有3组输入字段 我需要设置$errors,如果: 如果用户只填写长度并将高度/宽度/重量留空,则集合中只有部分字段(而不是所有字段)已填写完毕 如果填写的字段不是数字字段 如果一组字段只包含占位符文本height/weight/width/length,我还需要我的代码忽略这些字段 我如何使用附加的代码来实现这一点表单验证-PHP中的输入字段,php,arrays,input,foreach,validation,Php,Arrays,Input,Foreach,Validation,我有一个名为“test.php”的脚本,它有3组输入字段 我需要设置$errors,如果: 如果用户只填写长度并将高度/宽度/重量留空,则集合中只有部分字段(而不是所有字段)已填写完毕 如果填写的字段不是数字字段 如果一组字段只包含占位符文本height/weight/width/length,我还需要我的代码忽略这些字段 我如何使用附加的代码来实现这一点 <?php if (isset($_POST['submitform'])) { $errors = array();
<?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>
非常感谢。我很困惑。您的代码显示,$errors=array;,这意味着$errors不是布尔值。对不起,当我说TRUE时,我的意思是,它不是空的。我的错。你想要的结果的哪一部分是你没有达到的代码?嗨,帕克007。当前,如果集合中的字段未完成,则不会突出显示该集合中的错误。此外,验证不会忽略包含占位符文本的所有4个字段的集合。