表单验证中的php foreach循环
我已经完成了表单验证,我只需要在输入字段中应用一个CSS类,在触发错误时给它一个红色边框。有两种可能的错误。每一个都可以单独工作,但是当两个错误都被触发时,只有一个框得到边界。在另一个窗体上使用了相同的代码,但没有在函数中使用,因此效果很好。我错过了什么表单验证中的php foreach循环,php,forms,validation,foreach,Php,Forms,Validation,Foreach,我已经完成了表单验证,我只需要在输入字段中应用一个CSS类,在触发错误时给它一个红色边框。有两种可能的错误。每一个都可以单独工作,但是当两个错误都被触发时,只有一个框得到边界。在另一个窗体上使用了相同的代码,但没有在函数中使用,因此效果很好。我错过了什么 //function stackoverflow($pet, $grab, $errors_NaN) ... ... //testing here to make sure all errors are in the array var
//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
//函数stackoverflow($pet,$grab,$errors\u NaN)
...
...
//在此处测试以确保所有错误都在阵列中
变量转储($errors\u NaN);
回声“
”;
foreach($errors\u NaN作为$error){
echo$错误。“
”;
}
回声';
回声';
回声“重量”//无文本验证
回声';
回声“磅”;
回声';
回显“”;//结束形式行
回声';
呼应“年龄”//无文本验证
回声';
回音“年”;
回声';
回声'//结束形式行
在一些可能出现问题的地方发表评论替换:
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"';
}
为此:
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"';
}
}
}
要查看您犯错误的位置:
foreach($errors\u NaN as=>$error){
->foreach($errors\u NaN as$error){
如果weight\u NaN和age\u NaN都在数组中,您将同时触发它们(因此也使用else语句:echo'class=“weight”)
我想说,如果您使用以下选项:
$useClass = 'class="weight"';
if (!empty($errors_NaN)) {
foreach($errors_NaN as $error){
if($error == 'weight_NaN'){
$useClass 'class="edit_mfwa"';
}
}
}
echo $useClass;
它会正常工作:)!试试这个:
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 '" />';
echo';
回声';
echo“Weight”;//无文本验证
回声';
您是否检查过生成的html?检查我的awnser,我将其编辑为一个更好的示例:)不太了解三元运算符,因此无法调整您的代码。还有一个吗(在那里,我不知道该怎么办。很好。这是我在stackoverflow中编辑代码时犯的一个错误。这在我的实际代码中不是一个问题。是的,我希望你能找到解决方案乐于帮助,只要你知道你做错了什么,就可以从中吸取教训!