Reputation: 2363
I am using a PHP validation library. It only comes with an example of how to show a list of errors at the top of the form.
How can I make this so it will echo ' class="error"' in the label and form element for each field that is currently in error?
Form HTML:
<table>
<tr>
<td align="right"><label for="first_name">First Name</label></td>
<td>:</td>
<td><input type="text" name="first_name" id="first_name"></td>
</tr>
<tr>
<td align="right"><label for="last_name">Last name</label></td>
<td>:</td>
<td><input type="text" name="last_name" id="last_name"></td>
</tr>
<tr>
<td align="right"><label for="phone">Phone</label></td>
<td>:</td>
<td><input type="text" name="phone" id="phone"></td>
</tr>
<tr>
<td align="right"><label for="email">Email</label></td>
<td>:</td>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td align="right"><label for="state">State</label></td>
<td>:</td>
<td>
<select name="state">
<option value="">Select</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
</select>
</td>
</tr>
</table>
PHP Error Array Dump:
array(5) {
["first_name"]=>
string(30) "First Name is a required field"
["last_name"]=>
string(29) "Last Name is a required field"
["phone"]=>
string(25) "Phone is a required field"
["email"]=>
string(25) "Email is a required field"
["state"]=>
string(21) "Please select a state"
}
Upvotes: 0
Views: 860
Reputation: 6950
something like this
<?php
foreach($errorArray as $key=>$value)
{
$class = "$key_error";
$$class = "class=error";
}
?>
<table>
<tr>
<td align="right"><label for="first_name" <?php echo $first_name_error?>>First Name</label> </td>
<td>:</td>
<td><input type="text" <?php echo $first_name_error?> name="first_name" id="first_name"></td>
</tr>
<tr>
<td align="right"><label <?php echo $last_name_error?> for="last_name">Last name</label></td>
<td>:</td>
<td><input type="text" <?php echo $last_name_error?> name="last_name" id="last_name"></td>
</tr>
-------------
</tr>
</table>
Upvotes: 1