简体   繁体   English

挣扎于PHP,无法弄清楚我做错了什么

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

Alright, I'm probably missing something very obvious but I can't for the life of me figure out what it is.. 好吧,我可能会遗漏一些非常明显的东西,但是我无法终生弄清楚它是什么。

The problem I am having is that the information the user types into the input fields 'username' and 'password' don't get passed on when I use $_POST[''] 我遇到的问题是,当我使用$ _POST [“]时,用户输入到输入字段'username'和'password'中的信息不会传递

Here's the form I am using, very basic: 这是我使用的表格,非常基本:


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

And this is the PHP I have written, it is in the same document for testing purposes, the problem isn't a missing header. 这是我编写的PHP,它在同一文档中用于测试目的,问题不在于缺少标头。 I have tested it in seperate files, too, but no difference. 我也已经在单独的文件中对其进行了测试,但是没有区别。


<?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 />';
    }
  }
}

Thank you in advance, I feel stupid asking this but I would really appreciate any feedback! 预先谢谢您,我问这个问题很愚蠢,但是我非常感谢您提供任何反馈!

You are missing the action="login.php" since your file name is login.php in the <form> like this : 您缺少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>

and the php code is something like this 和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 />';
        }
    }
}

even if I think the first two tests are useless 即使我认为前两个测试都没有用

我使用的是PHPStorm版本,该版本具有与POST使用相关的错误,这就是GET起作用的原因。

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

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