Hugoar
Hugoar

Reputation: 41

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['']

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

Upvotes: 0

Views: 66

Answers (2)

Hugoar
Hugoar

Reputation: 41

I am using a version of PHPStorm which has a bug related to the use of POST, this is why GET did work.

Upvotes: 0

Omis Brown
Omis Brown

Reputation: 199

You are missing the action="login.php" since your file name is login.php in the <form> like this :

<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

$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

Upvotes: 1

Related Questions