简体   繁体   English

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

[英]Change the page when pressing a submit button in PHP

When I press the submit button "login" I want my page to go to "frontPage.php", but it goes instead to "login.php" even though I have specified当我按下提交按钮“登录”时,我希望我的页面到 go 到“frontPage.php”,但即使我已经指定,它也会转到“login.php”

header("location: frontPage.php");

in my login.php.在我的登录名中。php。

login.php login.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>

you are missing the attribute name of all inputs try this您缺少所有输入的属性名称试试这个

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

You are missing the name attribute on the submit button.您缺少提交按钮上的名称属性。 It should have an attribute of name=“login”.它应该具有 name="login" 的属性。

All of your inputs are also missing the name attribute.您的所有输入也缺少名称属性。 Copy what you have for id attributes for the missing name attributes.复制缺少的名称属性的 id 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM