0

我正在使用 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 错误数组转储:

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"
}
4

1 回答 1

1

像这样的东西

 <?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>
于 2013-03-23T04:49:48.117 回答