如何在此登录页面中添加内联表单验证

时间:2017-06-06 10:41:32

标签: php forms twitter-bootstrap

如何向此页面添加内联验证。目前,如果出现错误,它会回到新页面,您必须返回到。我该如何自动执行此操作。我赞同我的表格,我不确定这是不是很好的做法,所以任何改进建议都会受到高度赞赏。

if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)
{
    echo 'You are already signed in, you can <a href="Logour.php">sign out</a> if you want.';
}
else
{
    if($_SERVER['REQUEST_METHOD'] != 'POST')
    {
        /*the form hasn't been posted yet, display it
          note that the action="" will cause the form to post to the same page it is on */
            echo '
        <div class="container">
            <div id="login-form">
                <form method="post" action="">
                    <div classs="col-md-12">
                        <div class="form-group">
                            <h2>Sign Up</h2>
                        </div>
                        <div class="form-group">
                            <hr />
                        </div>

                        <div class="form-group">
                            <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                                <input type="text" name="userName" class="form-control" placeholder="Enter username" maxlength="50"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                                <input type="password" name="userPass" class="form-control" placeholder="Enter Password" maxlength="50"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <hr />
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-block btn-primary name="btn-signup">Sign In</button>
                        </div>
                        <div class="form-group">
                            <hr />
                        </div>
                        <div class="form-group">
                            <a href="Register.php">Register</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>'
             ;
    }
    else
    {
        /* so, the form has been posted, we'll process the data in three steps:
            1.  Check the data
            2.  Let the user refill the wrong fields (if necessary)
            3.  Varify if the data is correct and return the correct response
        */
        $errors = array(); /* declare the array for later use */

        if(!isset($_POST['userName']))
        {
            $errors[] = 'The username field must not be empty.';
        }

        if(!isset($_POST['userPass']))
        {
            $errors[] = 'The password field must not be empty.';
        }

1 个答案:

答案 0 :(得分:0)

if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)
{
    echo 'You are already signed in, you can <a href="Logour.php">sign out</a> if you want.';
}
else
{
if($_SERVER['REQUEST_METHOD'] != 'POST')
{ 
     /*the form hasn't been posted yet, display it
      note that the action="" will cause the form to post to the same page it is on */
?>       
    <div class="container">
        <div id="login-form">
            <form method="post" action="">
                <div classs="col-md-12">
                    <div class="form-group">
                        <h2>Sign Up</h2>
                    </div>
                    <div class="form-group">
                        <hr />
                    </div>

                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                            <input type="text" name="userName" class="form-control" placeholder="Enter username" maxlength="50"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                            <input type="password" name="userPass" class="form-control" placeholder="Enter Password" maxlength="50"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <hr />
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-block btn-primary name="btn-signup">Sign In</button>
                    </div>
                    <div class="form-group">
                        <hr />
                    </div>
                    <div class="form-group">
                        <a href="Register.php">Register</a>
                    </div>
                </div>
            </form>
        </div>
    </div> 
<?php
}
else
{
    /* so, the form has been posted, we'll process the data in three steps:
        1.  Check the data
        2.  Let the user refill the wrong fields (if necessary)
        3.  Varify if the data is correct and return the correct response
    */
    $errors = array(); /* declare the array for later use */

    if(!isset($_POST['userName']))
    {
        $errors[] = 'The username field must not be empty.';
    }

    if(!isset($_POST['userPass']))
    {
        $errors[] = 'The password field must not be empty.';
    }
相关问题