php - 当表单中发生错误时使文本框颜色为红色

标签 php html css forms

我需要你的帮助.. 我正在尝试在表单出现错误时将文本框设为红色...

这就是我能做的。但是当我提交 fore 时,我在表单中得到一个 Undefined Index error_css

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

表单

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

感谢您的宝贵时间...

最佳答案

尝试这样的事情:

<?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; ?>"/>

关于php - 当表单中发生错误时使文本框颜色为红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626160/

相关文章:

javascript - TinyMCE方向

php - session 适用于多个网站

PHP - 在 null 上调用成员函数

html - 仅通过 CSS 调整滚动条按钮位置

jquery - 如何防止使用 jQuery 提交表单?

javascript - 有没有办法动态创建嵌套的 div onclick?

php - 使用 Ajax 和 PHP 将数据发送到 MySQL

kohana - 还有其他 PHP 框架使用级联文件系统吗?

php - 快速提问 : How to set a cookie that will expire after 90days in PHP?

css - 绝对定位 TR 和 TD 元件