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
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 :)!
replace:
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"';
}
with this:
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"';
}
}
}
To see where u made mistake: foreach($errors_NaN as => $error){
-> foreach($errors_NaN as $error){
Try this:
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 '" />';