【发布时间】:2011-07-03 03:00:27
【问题描述】:
有人可以帮我修复此代码吗?我要解决的是:
如果一个人没有输入用户名或密码,我只想要错误文本(侧面的红色消息),顶部没有“抱歉,无法访问”消息。
另外,如果有人获得(或没有)访问权限,我希望文本字段和提交按钮消失。
这不会是一个真实的表格,所以请不要担心我如何使用用户名和密码......如果可能的话,你认为我的大部分代码可以保持不变吗?
谢谢!
<?php
echo '<style type="text/css">
.error
{
color: red;
}
</style>';
$error = false;
if (isset($_POST['submitted']))
{
if (empty($_POST['username']) || empty($_POST['password']))
{
$error = TRUE;
}
if (!$error && $_POST['username']=='test' && $_POST['password']=='abc123') {
echo '<p>Correct. Thank you for entering.<p/>';
}
else
{
echo '<p>Sorry, no access.</p>
';
}
}
?>
<form action="" method="post">
Username: <input type="text" name="username" size="20" value="<?php
if (isset($_POST['submitted']) && !empty($_POST['username']))
{
echo $_POST['username'];
} ?>" />
<?php
if (isset($_POST['submitted']) && empty($_POST['username']))
{
echo '<span class="error">Please enter a username.</span>';
}
?>
<br />Password: <input type="password" name="password" size="20" value="<?php
if (isset($_POST['submitted']) && !empty($_POST['password']))
{
echo $_POST['password'];
} ?>" />
<?php
if (isset($_POST['submitted']) && empty($_POST['password']))
{
echo '<span class="error">Please enter a password.</span>';
}
?>
<br /><input type="submit" value="Log in" />
<br /><input type="hidden" name="submitted" value="true" />
</form>
【问题讨论】:
标签: php webforms form-submit