php foreach循环在表单验证中

时间:2011-10-08 22:17:44

标签: php forms validation foreach

我已经完成了表单验证工作,我只需要在输入字段中应用CSS类,以便在触发错误时为其指定红色边框。有2个可能的错误。每个都可以单独工作,但是当两个错误都被触发时,只有一个方框得到边框。在另一个表单上使用相同的代码,但在函数中没有,它工作正常。我错过了什么?

//function stackoverflow($pet, $grab, $errors_NaN)
...
...
//testing here to make sure all errors are in the array
    var_dump($errors_NaN);
        echo '<br />';
        foreach($errors_NaN as $error){
            echo $error . '<br />';
        }


        echo '<div class="edit_col3">';
        echo '<div class="formrow">';
        echo '<label for="weight">Weight</label>'; //no text validation
            echo '<input ';


            if (!empty($errors_NaN)) {
                foreach($errors_NaN as $error){
                    if($error == 'weight_NaN'){
                        echo 'class="edit_mfwa"';
                                              //this triggers if ONLY weight_NaN is in the array but not if both weight_NaN and age_NaN
                    }
                    else {
                        echo 'class="weight"'; 
                    }
                }
            }
            elseif (empty($errors_NaN)){
                echo 'class="weight"';
            }


            echo ' type="text" name="weight" value="' . $grab['weight'] . '" id="weight" />';
            echo '<div class="pet_units">lbs</div>';
            echo '<div class="clear"></div>';
        echo '</div>';// end form row

        echo '<div class="formrow">';
        echo '<label for="age">Age</label>';  //no text validation
            echo '<input ';


            if (!empty($errors_NaN)) {
                foreach($errors_NaN as => $error){
                    if($error == 'age_NaN'){
                        echo 'class="edit_mfwa"';
                                              //this triggers properly in both situation
                    }
                    else {
                        echo 'class="age"';
                    }
                }
            } 
            elseif (empty($errors_NaN)) {
                echo 'class="age"';
            }


            echo ' type="text" name="age" value="' .  $grab['age'] . '" id="age" />';
            echo '<div class="pet_units">years</div>';
            echo '<div class="clear"></div>';
        echo '</div>'; //end form row

在一些问题似乎正在发生的地方评论

3 个答案:

答案 0 :(得分:1)

如果weight_NaN和age_NaN都在你的数组中,你将触发两者(所以也使用你的else语句:echo'class =“weight”';

我想说,如果你使用如下:

        $useClass = 'class="weight"';

        if (!empty($errors_NaN)) {
            foreach($errors_NaN as $error){
                if($error == 'weight_NaN'){
                    $useClass 'class="edit_mfwa"';
                }
            }
        }

        echo $useClass;

它会正常工作:)!

答案 1 :(得分:1)

试试这个:

echo '<div class="edit_col3">';
echo '<div class="formrow">';
echo '<label for="weight">Weight</label>'; //no text validation
echo '<input class="'.(in_array('weight_NaN', $errors_NaN)?'edit_mfwa':'weight';
echo '" />';

答案 2 :(得分:0)

取代:

if (!empty($errors_NaN)) {
            foreach($errors_NaN as => $error){
                if($error == 'age_NaN'){
                    echo 'class="edit_mfwa"';
                                          //this triggers properly in both situation
                }
                else {
                    echo 'class="age"';
                }
            }
        } 
        elseif (empty($errors_NaN)) {
            echo 'class="age"';
        }

用这个:

if (!empty($errors_NaN)) {
                foreach($errors_NaN as $error){
                    if($error == 'age_NaN'){
                        echo 'class="edit_mfwa"';
                                              //this triggers properly in both situation
                    }
                    else {
                        echo 'class="age"';
                    }
                }
            } 

要查看您犯了哪些错误:foreach($errors_NaN as => $error){ - &gt; foreach($errors_NaN as $error){