Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP POST not working but GET works

Tags:

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>
like image 811
keziakoko Avatar asked Aug 29 '17 08:08

keziakoko


People also ask

Why post method is not working?

The Request Method' POST' Not Supported error is caused by a mismatch of the web browser configuration and the browser's URL format. In this case, the browser sends a URL request, the web server receives and recognizes the URL but cannot execute commands or grant access to the requested page.

Which is better POST or GET?

GET performs are better compared to POST because of the simple nature of appending the values in the URL. It has lower performance as compared to GET method because of time spent in including POST values in the HTTP body. This method supports only string data types.

How does POST work in PHP?

In the PHP POST method, data from HTML FORM is submitted/collected using a super global variable $_POST. This method sends the encoded information embedded in the body of the HTTP request and hence the data is not visible in the page URL unlike the GET Method.

What is the difference between POST and GET?

Both GET and POST method is used to transfer data from client to server in HTTP protocol but Main difference between POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from client to ...


1 Answers

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.

like image 157
Arfan Mahmood Avatar answered Oct 19 '22 10:10

Arfan Mahmood