duanchi3109 2017-08-29 08:46
浏览 71
已采纳

PHP POST不工作,但GET工作

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>
  • 写回答

2条回答 默认 最新

  • ds122455 2017-08-29 09:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)