gpt4 book ai didi

php - 如何回显错误类以标记和形成有错误的元素?

转载 作者:行者123 更新时间:2023-12-03 08:34:03 25 4
gpt4 key购买 nike

我正在使用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"
}

最佳答案

像这样的东西

 <?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>

关于php - 如何回显错误类以标记和形成有错误的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15583391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com