douxie2029 2011-10-08 22:17
浏览 24
已采纳

php foreach循环在表单验证中

I already got the form validation working, I just need to apply a CSS class to the input field to give it a red border when an error was triggered. There are 2 possible errors. Each one works fine individually but when both errors are triggered only one of the boxes get the border. Used this same code on another form but not in a function and it worked fine. What am I missing?

//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

Commented in a few places where the problem seems to be happening

  • 写回答

3条回答 默认 最新

  • dongqufi82315 2011-10-08 22:23
    关注

    If both weight_NaN and age_NaN are in your array, you will trigger both (so also using your else statement: echo 'class="weight"';

    I would say, if you use as following:

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

    It will work proper :)!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)