我正在使用PHP验证库。它只提供了一个如何在表单顶部显示错误列表的示例。
我怎么能做到这一点,所以它会回声' class="error"'在标签和表单元素的每个字段,目前是错误的?
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"
}
像这样
<?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>