我需要你的帮助。每当表单出现错误时,我都会尝试使文本框变红......
这就是我能够做到的。但是当我提交前额时,我得到了一个未定义的索引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; ?>"/>
如果可以,为什么不研究像jQuery验证插件这样的解决方案呢?这不是一个完整的解决方案,因为您仍然需要某种服务器端验证,但它会让您继续前进。
你从不在 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' >
表单
<label for="username">Username:</label>
<input id="username" type="text" value="<?php echo @$_POST['username']; ?>"
name="username" title='Username' style="<?php echo @$error_css; ?>"/>
您的变量$error_css
为空/不存在。
我认为脚本开头的 2 个 if 案例永远不会达到$error_css
获得其内容的地步。