我已經得到了表單驗證的工作,我只需要在輸入字段中應用一個CSS類,在觸發錯誤時給它一個紅色邊框。有2個可能的錯誤。每個單獨工作都很好,但是當兩個錯誤都被觸發時,只有其中一個框出現邊界。在另一個表單上使用相同的代碼,但不是在一個函數中,它工作正常。我錯過了什麼?php foreach循環在表單驗證
//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
在這個問題似乎有幾個地方評論是發生
你有沒有檢查生成的HTML? –
檢查我的awnser,我編輯它以更好的示例:) – Wesley