在每个字段旁边显示表单验证错误 [英] Display form validation errors next to each field
问题描述
我正在编写一个用php编写的简单联系表单,几乎所有的设置都是我想要的,现在除了现在,所有验证错误都显示在表单顶部的div上,我想要更改下面的代码,以便它们显示在导致错误的表单字段旁边,但我不知道如何完成此操作,因此我请专家提供一些建议。
PHP验证例程
$ frm_valid = new Validate();
if(isset($ _ POST ['submit'])){
$ rules = array(
array('Type'=>'Required' ,'Var'=> $ _ POST ['name'],'Msg'=&';'Name field is required。'),
array('Type'=&';'''''Var'=' > $ _ POST ['email'],'Msg'=>'需要电子邮件地址字段'),
array('Type'=&'Email','Var'=> $ _ POST '''','Msg'=>'您的电子邮件地址格式无效'),
array('Type'=&';'Required','Var'=> $ _ POST ['subject '','Msg'=>'Subject field is required。'),
array('Type'=>'Required','Var'=> $ _ POST ['message'],'Msg '=>'消息字段是必需的'),
);
$ result = $ frm_valid->有效($ rules);
if(is_array($ result)){
//打印验证错误(如果有)
echo('< div>< b>表单不能< / b>< ul>');
foreach($ result as $ key => $ val){
echo'< ; li>'。$ val。'< / li>';
}
echo('< / ul>< / div>< br />');
}
else {
//做别的事。
}
}
表单HTML
< form action =< ;? echo htmlentities($ _ SERVER ['PHP_SELF']); ?>method =post>
< input type =textname =namevalue =/>< br />
< ; input type =textname =emailvalue =/>< br />
< input type =textname =subjectvalue = />< br />
< textarea name =messagecols =80rows =7>< / textarea>< br />
< / form>
您需要为此更改整个代码!!!结构可能是这样的:
< form action =<?php echo htmlentities($ _ SERVER ['PHP_SELF']); > $>
< ul>
< li>
< label>
< span>名称< / span>
<输入type =textname =name/>
< small class =errorText><?php echo($ _POST [name] ==)?这个字段是必填:;?>< / small>
< / label>
< / li>
< li>
< label>
< span>电子邮件< / span>
<输入类型=textname =email/>
< small class =errorText><?php echo ($ _POST [email] ==)?此栏位为必填项:;?>< / small>
< / label>
< / li>
< li>
< label>
< span> Subject< / span>
< input type =textname =subject/>
< small class =errorText><?php echo($ _POST [subject] ==)? 这是必填栏 : ; ?>< /小>
< / label>
< / li>
< li>
< label>
< span>讯息< / span>
< small class =errorText><?php echo($ _POST [message] ==)? 这是必填栏 : ; ?>< /小>
< / label>
< / li>
< li>
< input type =submitname =submitvalue =Send/>
< / li>
< / ul>
< / form>
与CSS相同:
* {font-family:Segoe UI,Tahoma;}
h1 {font-weight:bold; font-size:14pt;填充:5px 0; margin:5px 0;边界:1px固体#999; border-width:1px 0;}
input [type ='submit'] {padding:5px 20px; cursor:pointer;}
ul,li {display:block; list-style:none;保证金:0; padding:0;}
ul li {padding:5px 0;}
ul li label span {display:block; cursor:pointer;}
ul li label .errorText {color:#f00; font-weight:bold; vertical-align:top;}
ul li label textarea {width:300px;}
您可以在这里看到现场演示:演示
PHP中的错误处理
为每个字段保留一个错误变量。说,
<?php
$ error = array(
name=> ,
email=>,
subject=>,
message=>
);
?>
更新错误并在下面显示。
<?php
if(empty()$ _ POST [email])
$ error [email] =需要电子邮件!;
elseif(!isEmail($ _ POST [email]))
$ error [email] =不是有效的电子邮件!
?>
如果没有错误,它将是空的,用户看不到错误信息。在您的表单代码中,您需要更新这种方式:
< small class =errorText>< ;? php echo $ error [name]; ?>< /小>
< small class =errorText><?php echo $ error [email]; ?>< /小>
在后端, I am working on a simple contact form written in php and have almost everything setup as I would like for it to be except right now, all validation errors are displaying in a div on the top of my form, and I would like to change the code below so they are displayed next to the form field that caused the error, but I am not sure how to accomplish this so I have come to the experts for some advise. PHP Validation Routine Forms HTML
You need to change the whole code for this!!! The structure, perhaps this way: And the CSS for the same: You can see a live demo here: Demo Keep an error variable for each field. Say, Update them with the errors and display them below. If there are no errors, it would be empty and the user doesn't see the error message. In your Forms Code, you need to just update this way: where in the backend, the 这篇关于在每个字段旁边显示表单验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! $ error [email]
应该有
这个字段是必需的
或不是有效的电子邮件地址!
。 $frm_valid = new Validate();
if(isset($_POST['submit'])){
$rules=array(
array('Type'=>'Required','Var'=>$_POST['name'],'Msg'=>'Name field is required.'),
array('Type'=>'Required','Var'=>$_POST['email'],'Msg'=>'Email address field is required.'),
array('Type'=>'Email','Var'=>$_POST['email'],'Msg'=>'Your email address format is invalid.'),
array('Type'=>'Required','Var'=>$_POST['subject'],'Msg'=>'Subject field is required.'),
array('Type'=>'Required','Var'=>$_POST['message'],'Msg'=>'Message field is required.'),
);
$result = $frm_valid->Valid($rules);
if(is_array($result)){
// Print validation errors (if any)
echo('<div"><b>The form cannot be submitted until the following errors are corrected.</b><ul>');
foreach($result as $key=>$val){
echo '<li>'.$val.'</li>';
}
echo('</ul></div><br />');
}
else {
// Do something else.
}
}
<form action="<? echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" name="name" value="" /> <br />
<input type="text" name="email" value="" /> <br />
<input type="text" name="subject" value="" /> <br />
<textarea name="message" cols="80" rows="7"></textarea> <br />
<input type="submit" name="submit" value="Send" />
</form>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<ul>
<li>
<label>
<span>Name</span>
<input type="text" name="name" />
<small class="errorText"><?php echo ($_POST["name"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Email</span>
<input type="text" name="email" />
<small class="errorText"><?php echo ($_POST["email"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Subject</span>
<input type="text" name="subject" />
<small class="errorText"><?php echo ($_POST["subject"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Message</span>
<textarea name="message" cols="80" rows="7"></textarea>
<small class="errorText"><?php echo ($_POST["message"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<input type="submit" name="submit" value="Send" />
</li>
</ul>
</form>
* {font-family: Segoe UI, Tahoma;}
h1 {font-weight: bold; font-size: 14pt; padding: 5px 0; margin: 5px 0; border: 1px solid #999; border-width: 1px 0;}
input[type='submit'] {padding: 5px 20px; cursor: pointer;}
ul, li {display: block; list-style: none; margin: 0; padding: 0;}
ul li {padding: 5px 0;}
ul li label span {display: block; cursor: pointer;}
ul li label .errorText {color: #f00; font-weight: bold; vertical-align: top;}
ul li label textarea {width: 300px;}
Error Handling in PHP
<?php
$error = array(
"name" => "",
"email" => "",
"subject" => "",
"message" => ""
);
?>
<?php
if (empty()$_POST["email"])
$error["email"] = "Email is required!";
elseif (!isEmail($_POST["email"]))
$error["email"] = "Not a Valid Email!";
?>
<small class="errorText"><?php echo $error["name"]; ?></small>
<small class="errorText"><?php echo $error["email"]; ?></small>
$error["email"]
would have either "This field is required"
or "Not a valid email address!"
.