当提交按钮时,PHP并不知道

时间:2014-11-10 03:43:52

标签: php redirect button submit

    <html>
 <div class="panel-body">
                        <form method="post" action="index.php">
                            <div class="form-group">
                                <input class="form-control" placeholder="username" name="username" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="password" name="password" type="password" value="">
                            </div>
                            <input type='submit' name="submit" value='Login'>
                        </form>
                        <?php
                        if(isset($_POST['submit']))
                        {    
                            $username = sanitize($_POST['username']);
                            $password = sanitize($_POST['password']);
                            if($username && $password)
                            {
                                $query = mysql_query("SELECT Name, Password FROM users WHERE Name = '$username' LIMIT 1");
                                if(mysql_num_rows($query) == 1)
                                {
                                    while($row = mysql_fetch_assoc($query))
                                    {
                                        $dbusername = $row['Name'];
                                        $dbpassword = $row['Password'];
                                    }
                                    $somename = hash( 'whirlpool', $password);
                                    $somename = strtoupper($somename);
                                    if($username == $dbusername && $somename == $dbpassword)
                                    {
                                        $_SESSION['username'] = $dbusername;
                                        header('location: /pcp/home.php');
                                    }
                                    else $error = "Wrong password!";
                                }
                                else $error =  "Username doesn't exist!";
                            }
                            else $error = "Type name and password!";
                        }
                        ?>
                    </div>
</html>

当我使用正确的密码和用户提交按钮时,它不会转到home.php,但是当我重新加载时,它会继续。

它正在使用bootstrap,这是为什么?如果是这样,你能帮我解决吗?

1 个答案:

答案 0 :(得分:2)

如果您想将PHP代码与表单元素一起添加,则需要编写

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
是不是。只需单独的表格页面&amp; php页面。 这意味着

的index.php

  <html>
 <div class="panel-body">
                        <form method="post" action="check.php">
                            <div class="form-group">
                                <input class="form-control" placeholder="username" name="username" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="password" name="password" type="password" value="">
                            </div>
                            <input type='submit' name="submit" value='Login'>
                        </form>

                    </div>
</html>

ckeck.php

               <?php
                if(isset($_POST['submit']))
                {    
                    $username = sanitize($_POST['username']);
                    $password = sanitize($_POST['password']);
                    if($username && $password)
                    {
                        $query = mysql_query("SELECT Name, Password FROM users WHERE Name = '$username' LIMIT 1");
                        if(mysql_num_rows($query) == 1)
                        {
                            while($row = mysql_fetch_assoc($query))
                            {
                                $dbusername = $row['Name'];
                                $dbpassword = $row['Password'];
                            }
                            $somename = hash( 'whirlpool', $password);
                            $somename = strtoupper($somename);
                            if($username == $dbusername && $somename == $dbpassword)
                            {
                                $_SESSION['username'] = $dbusername;
                                header('location: /pcp/home.php');
                            }
                            else $error = "Wrong password!";
                        }
                        else $error =  "Username doesn't exist!";
                    }
                    else $error = "Type name and password!";
                }
                ?>