当表单中出现错误时,使文本框颜色为红色 [英] Making textbox color red when error occurs in a form

查看:470
本文介绍了当表单中出现错误时,使文本框颜色为红色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助..我想让文本框的红色,当一个窗体中有错误...



这是我能做到。但是当我提交fore,我得到一个未定义的索引 error_css 在形式

  if ($ _POST ['submit'])){
if(empty($ _ POST ['username'])){
$ error_css ='background-color:red'
}

表单

 < label for =username>用户名:< / label> 
< input id =usernametype =textvalue =<?php if(isset($ _ POST ['username'])){echo $ _POST ['username'];}? ; name =usernametitle ='Username'/>感谢您的时间...



$ b >解决方案

尝试这样:

 <?php 

$ username =;
$ error_css =;

if(isset($ _ POST ['submit'])){
if(isset($ _ POST ['username']))
$ username = $ _POST ['用户名'];
else
$ error_css ='background-color:red';
}

?>

< label for =username>用户名:< / label>
< input id =usernametype =textvalue =<?php echo $ username;?> name =usernametitle ='Username'style =<?php echo $ error_css;?>/>


I need your help.. I'm trying to make the textbox red whenever there's an error in a form...

This is what i've being able to do. But when i submit the fore, I get an Undefined Index error_css in the form

 if (isset($_POST['submit'])) { 
 if (empty($_POST['username'])) {
       $error_css='background-color:red';
 }

Form

<label for="username">Username:</label>
<input id="username" type="text" value="<?php if(isset($_POST['username'])){ echo $_POST['username']; } ?>" name="username" title='Username' />

thanks for your time...

解决方案

Try something like this:

<?php

$username = "";
$error_css = "";

if (isset($_POST['submit'])) {
    if(isset($_POST['username']))
        $username = $_POST['username'];
    else
        $error_css='background-color:red';
}

?>

<label for="username">Username:</label>
<input id="username" type="text" value="<?php echo $username; ?>" name="username" title='Username' style="<?php echo $error_css; ?>"/>

这篇关于当表单中出现错误时,使文本框颜色为红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆