在 PHP 中按下提交按钮时更改页面

时间:2021-01-16 05:20:23

标签: php html

当我按下提交按钮“登录”时,我希望我的页面转到“frontPage.php”,但即使我已指定,它也会转到“login.php”

header("location: frontPage.php");

在我的 login.php 中。

登录.php

if (isset($_POST['login'])) {
        if (empty($_POST["username"]) || empty($_POST["password"])) {
            echo "Please fill all fields";
        } else {
            $usernameinput = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);
            $passwordinput = filter_input(INPUT_POST, "password", FILTER_SANITIZE_STRING);
            
            $query = "SELECT * FROM users WHERE username = :username";
            $stmt  = $conn->prepare($query);
            $stmt->execute(array(
                'username' => $usernameinput
            ));
            $count = $stmt->rowCount();
            
            if ($count > 0) {
                
                while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    
                    if (password_verify($passwordinput, $result["password"])) {
                        $_SESSION["username"] = $usernameinput;
                        break;
                    }
                }
                header("location: frontPage.php");
            } else {
                header("location: index.php");
            }
        }
    }
<body>
    <div class="container">
        <form id="form" class="form" method="POST" action="login.php">
            <h2>Log In</h2>
            <div class="form-control">
                <label for="username">Username:</label>
                <input type="text" id="username" placeholder="Enter Username">
            </div>
            <div class="form-control">
                <label for="password">Password:</label>
                <input type="password" id="password" placeholder="Enter Password">
            </div>
            <button id="login">Submit</button>
        </form>
    </div>
</body>

2 个答案:

答案 0 :(得分:1)

您缺少所有输入的属性名称 试试这个

<body>
    <div class="container">
        <form id="form" class="form" method="POST" action="login.php">
            <h2>Log In</h2>
            <div class="form-control">
                <label for="username">Username:</label>
                <input type="text" id="username" name="username" placeholder="Enter Username">
            </div>
            <div class="form-control">
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" placeholder="Enter Password">
            </div>
            <button id="login" type="submit" name="login">Submit</button>
        </form>
    </div>
</body>

答案 1 :(得分:0)

您缺少提交按钮上的名称属性。它应该有一个 name="login" 属性。

您的所有输入也都缺少 name 属性。为缺少的名称属性复制 id 属性的内容。