3

我需要你的帮助..每当表单出现错误时,我都会尝试使文本框变为红色...

这是我能够做到的。但是当我提交前台时,我在表单中得到一个未定义索引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' />

谢谢你的时间...

4

5 回答 5

0

如果可以,为什么不研究像jQuery 验证插件这样的解决方案呢?这不是一个完整的解决方案,因为您仍然需要某种服务器端验证,但它会让您顺利上路。

于 2012-05-16T20:31:43.400 回答
0

形式

<label for="username">Username:</label>
<input id="username" type="text" value="<?php echo @$_POST['username']; ?>"
   name="username" title='Username' style="<?php echo @$error_css; ?>"/>
于 2012-05-16T20:32:50.963 回答
0

您永远不会在 html 标记中设置样式,并且第一个 if{if{}} 语句是多余的。它应该是:

<label for="username">Username:</label>
    <input type="text" <?php 
        if(isset($_POST['username'])) { 
            echo 'value="'.$_POST['username'].'"'; 
        } else {
            echo 'style="background-color:red"';
        }
    ?> name="username" title='Username' >
于 2012-05-16T20:34:29.217 回答
0

您的变量$error_css为空/不存在。

$error_css我认为脚本开头的 2 个 if 案例永远不会达到获取其内容的地步。

于 2012-05-16T20:29:46.337 回答
0

尝试这样的事情:

<?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; ?>"/>
于 2012-05-16T20:45:46.507 回答