Skip to content
Advertisement

PHP POST not working but GET works

I have been struggling with this weird issue for too long now.

I have searched through all the so posts of the same issue and none of the solutions have helped.

I have an HTML form that is using the post method to log a user in. The form resubmits to the same page and PHP then checks if the submit button has been clicked and will then execute the appropriate code.

The problem is that my $_POST variable is empty, but if I use the GET method it works.

When I VAR_DUMP ( $_POST ) it returns array(0) { } proving that nothing is getting posted. Using $_REQUEST does not work either. All my form fields have a name attribute. I am using XAMPP to host the files and I access it through localhost in my browser http://localhost:8080/project/admin.php

I feel like I am missing something really simple but I can’t figure it.

Here is my code

admin.php

<?php
$failed = false;

VAR_DUMP ( $_POST );
if (isset($_POST["loginBtn"]))
{
    echo "<br>working";
    loginUser();
}

function loginUser()
{
    include 'dbconfig.php';//makes connection to the database

    $user = $_POST['username'];
    $pass = $_POST['password'];

    $pass = hash("sha256", $pass);
    $sql = $conn->prepare("SELECT * FROM users WHERE username=? AND password =?");
    $sql->bind_param("ss", $user, $pass);
    $sql->execute();
    $result = $sql->get_result();

    if (mysqli_num_rows($result) > 0)
    {
        echo "In match";
        session_start();

        while ($row = $result->fetch_assoc())
        {
            $id_v = $row["userID"];
        }

        $_SESSION['userId'] = $id_v;
        $_SESSION['sessionId'] = session_id();

        header('Location: newPost.php');

    }
    else
    {
        $failed = true;
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Admin</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="shortcut icon" href="resources/images/favicon.png">
    <link rel="stylesheet" href="css/style.css">
</head

<body>
<section class="grey-bg">
        <div class="container">
            <div class="title mb-50 center">
                <h2>Admin Login.</h2>
            </div>
            <div class="row">
                <div class="section-content">
                    <form action="admin.php" method="POST" role="form" id="form_login" name="form_login" >
                        <div class="form-group">
                            <label for="username">Username</label>
                            <input class="form-control" type="text" name="username" id="username"
                                   placeholder="Username"
                                   data-required>
                        </div>
                        <div class="form-group">
                            <label for="password">Password</label>
                            <input class="form-control" type="password" name="password" id="password"
                                   placeholder="Password"
                                   data-required>
                        </div>
                        <div class="form-group">
                            <input class="btn btn-color-out" type="submit" id="loginBtn" name="loginBtn" value="Login">
                        </div>

                        <?php
                            if($failed)
                            {
                                echo "<span id='errorMsg' style='color: red;'>Username and password do not match</span><br/><br/>";
                            }
                        ?>
                    </form>
                </div>
            </div>
        </div>
    </section>
</body>
</html>

Advertisement

Answer

Please make sure the “enable_post_data_reading” variable is set to “on” in php.ini. I have tried the same script on MAMP and it’s working fine. So, It’s your server settings issue.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement