表单值变量和提交问题

时间:2011-07-03 03:00:27

标签: php webforms form-submit

有人可以帮我修复此代码吗?我想解决的是:

如果某人没有输入用户名或密码,我只想要错误文本(侧面的红色消息),而不是“抱歉,无法访问”消息。

此外,如果一个人获得访问权限(或没有访问权限),我希望文本字段和提交按钮消失。

这不是一个真实的形式,所以请不要担心我如何使用用户名和密码....如果它可能你认为我的大多数代码可以保持不变?

谢谢!

    <?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>

2 个答案:

答案 0 :(得分:0)

试试这个:

<style type="text/css">
.error {
color: red;
}
</style>
<?php

$submitted  = isset($_POST['submitted']);
$userName   = isset($_POST['username']) ? $_POST['username'] : null;
$password   = isset($_POST['password']) ? $_POST['password'] : null;

if($submitted) {
    if (!$userName || !$password) {
        echo '<p class="error">Please go back and fill the inputs.</p>';
    } elseif($userName == 'test' && $password == 'abc123') {
        echo '<p>Correct. Thank you for entering.<p/>';
    } else {
        echo '<p class="error">Sorry, no access.</p>';
    }
} else {
?>
<form action="" method="post">
Username: <input type="text" name="username" size="20" value="<?php echo $userName; ?>" />
<br />
Password: <input type="password" name="password" size="20" value="" />
<br /><input type="submit" value="Log in" />
<br /><input type="hidden" name="submitted" value="true" />
</form>
<?php } ?>

答案 1 :(得分:0)

考虑在表单上使用onSubmit事件。

你需要在这里结合php和javascript以防止它提交。创建一个由onSubmit调用的Jscript函数。如果未填写值,则返回false。它将终止提交按钮并直接在屏幕上打印出来。

所以就这样:

$submitted  = isset($_POST['submitted']);
$userName   = isset($_POST['username']) ? $_POST['username'] : null;
$password   = isset($_POST['password']) ? $_POST['password'] : null;

if($submitted)
{
     if (!$userName || !$password) {
        echo '<p class="error">Please go back and fill the inputs.</p>';
    }
}

然后在你的表格上: <form action="" method="post" onSubmit="checkForm()">

然后弄清楚你将如何使用javascript检查表单。你可以将其余部分拼凑起来。