簡體   English   中英

掙扎於PHP,無法弄清楚我做錯了什么

[英]Struggling with PHP, can't figure out what I did wrong

好吧,我可能會遺漏一些非常明顯的東西,但是我無法終生弄清楚它是什么。

我遇到的問題是,當我使用$ _POST [“]時,用戶輸入到輸入字段'username'和'password'中的信息不會傳遞

這是我使用的表格,非常基本:


    <form method="post">

    <div class="container">
        <label><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="username" required>

        <label><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="password" required>

        <button type="submit">Login</button>
        <input type="checkbox" checked="checked"> Remember me
    </div>

    <div class="container" style="background-color:#f1f1f1">
        <button type="button" class="cancelbtn">Cancel</button>
        <span class="psw">Forgot <a href="#">password?</a></span>
    </div>
</form>

這是我編寫的PHP,它在同一文檔中用於測試目的,問題不在於缺少標頭。 我也已經在單獨的文件中對其進行了測試,但是沒有區別。


<?php
$username = 'username';
$password = '123';

if ($username == 'username') {
echo 'username retrieved <br />';
}
if ($password == '123') {
echo 'password retrieved <br />';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo ' method retrieved <br />';

if (isset($_POST['username']) && isset($_POST['password'])) {
    $u_username = $_POST['username'];
    echo $u_username . $username . '<br />';

    $u_password = $_POST['password'];
    echo $u_password . $password . '<br />';

    if ($u_username === $username && $u_password === $password) {
        echo 'username and password retrieved <br />';
    } else {
        echo 'username and/or password not retrieved <br />';
    }
  }
}

預先謝謝您,我問這個問題很愚蠢,但是我非常感謝您提供任何反饋!

您缺少action="login.php"因為您的文件名在<form>是如下所示的login.php:

<form method="post" action="login.php">
    <div class="container">
        <label><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="username" required>

        <label><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="password" required>

        <button type="submit">Login</button>
        <input type="checkbox" checked="checked"> Remember me
</div>

    <div class="container" style="background-color:#f1f1f1">
        <button type="button" class="cancelbtn">Cancel</button>
        <span class="psw">Forgot <a href="#">password?</a></span>
    </div>
</form>

和PHP代碼是這樣的

$username = 'username';
$password = '123';


if ($username == 'username') {
    echo 'username retrieved <br />';
}
if ($password == '123') {
    echo 'password retrieved <br />';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo ' method retrieved <br />';

    if (isset($_POST['username']) && isset($_POST['password'])) {
        $u_username = $_POST['username'];
        echo $u_username . $username . '<br />';

        $u_password = $_POST['password'];
        echo $u_password . $password . '<br />';

        if ($u_username == $username && $u_password == $password) {
            echo 'username and password retrieved <br />';
        } else {
            echo 'username and/or password not retrieved <br />';
        }
    }
}

即使我認為前兩個測試都沒有用

我使用的是PHPStorm版本,該版本具有與POST使用相關的錯誤,這就是GET起作用的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM