2011-10-08 112 views
0

我已經得到了表單驗證的工作,我只需要在輸入字段中應用一個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 

在這個問題似乎有幾個地方評論是發生

+0

你有沒有檢查生成的HTML? –

+0

檢查我的awnser,我編輯它以更好的示例:) – Wesley

回答

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; 

它將工作正確的:)!

+0

像一個魅力工作。謝謝! –

+1

高興地幫助,只要你知道你做錯了什麼,從中學習! – Wesley

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"'; 
        } 
       } 
      } 

要看到其中u犯的錯誤:foreach($errors_NaN as => $error){ - >foreach($errors_NaN as $error){

+0

好的趕上。這是我在編輯stackoverflow中的代碼時犯的一個錯誤。在我的實際代碼中沒有問題。 –

+0

是啊,我希望你找到解決方案 – Dzoki

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 '" />'; 
+0

不知道三元運營商非常好,所以無法調整您的代碼。有一個額外的(在那裏我不知道該怎麼辦。 –