在php中检查是否设置了$POST时如何使用$\u GET

在php中检查是否设置了$POST时如何使用$\u GET,php,post,get,Php,Post,Get,我有一个带有change password.php?id=1&key=37759的url 我试图从if语句中的url和usind中获取值,但遇到了一个问题 if (isset($_POST['password'])) { $id = $_GET['id']; $key = $_GET['key']; echo $key; // this is empty } 但如果我在外面回音,它是这样工作的: $id = $_GET['id']; $key =

我有一个带有change password.php?id=1&key=37759的url

我试图从if语句中的url和usind中获取值,但遇到了一个问题

if (isset($_POST['password'])) {
        $id = $_GET['id'];
        $key = $_GET['key'];
        echo $key; // this is empty
}
但如果我在外面回音,它是这样工作的:

$id = $_GET['id'];
$key = $_GET['key'];
echo $key; // I get the value
if (isset($_POST['password'])) {
        
}
这个也不起作用:

$id = $_GET['id'];
$key = $_GET['key'];
if (isset($_POST['password'])) {
        echo $key; // this is empty
}
这是此页面的完整代码 change password.php?id=1&key=37759是从用户请求重置密码时在其电子邮件中获得的链接设置的。然后,它将使用下面的代码将他们带到这个页面

    if (isset($_POST['password'])) {
        $id = $_GET['id'];
        $key = $_GET['key'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        
        $number = 4009;
        echo "test".$key.$number; // here I get test4009;
        exit();
        if ($password !== $confirm_password) {
            echo "Password do not match";
            exit();
        }
        
        $sql = "SELECT * FROM password_reset WHERE id='$id' AND key_check='$key'";
        
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        if(mysql_num_rows($result) < 1) {
            echo "Check your email to verify that you have received the reset passoword link or try to resend the link again";
        } else if($row['expired'] == true) {
            echo "The link has expired";
        } else {
            $sql1 = "UPDATE password_rest SET expired=true WHERE id='$id'";
            $sql2 = "UPDATE user SET password='$password' WHERE id='$id'";
            if (mysqli_query($conn, $sql1) && mysqli_query($conn, $sql2)) {
                echo "<span class='text-success'>password updated successfully</span>";
                exit();
            } else {
                echo "Failed to update password";
            }
        }
        
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Register </title>

    <!-- Bootstrap core CSS -->
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">


    <!-- Custom styles for this template -->
    <link href="css/shop-homepage.css" rel="stylesheet">

    <style>
    .form-inline {
        display: none;
    }
    </style>

</head>

<body>

    <?php include('includes/header.php'); ?>

    <!-- Page Content -->
    <div class="container">
        <!-- /.col-lg-3 -->

        <div class="form w-60 m-auto mt-2">
            <h4 style="margin-top: 0.4em" class="text-center">Reset Password</h4>
            <form id="form-data" action="sell_parser.php" method="post" enctype="multipart/form-data">
                
                <div class="form-group">
                    <label for="password">Password:</label>
                    <input type="password" class="form-control" id="password" placeholder="password" name="password">
                </div>

                <div class="form-group">
                    <label for="confirm_password">Confirm Password:</label>
                    <input type="password" class="form-control" id="confirm_password" placeholder="confirm password"
                        name="confirm_password">
                </div>
                <button type="submit" id="submit" class="btn btn-primary mb-2 btn-bg">Submit</button>
            </form>
            <p>To re-send the reset password link please? <a href="/afrolic/login.php">Click here</a></p>
            <div style="min-height: 12px;">
                <span id="results" class="text-danger"></span>
            </div>
        </div>

    </div>
    <!-- /.container -->

    <!-- Bootstrap core JavaScript -->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script>
    $(document).ready(function() {
        $("#form-data").submit(function() {
            $.ajax({
                url: "change-password.php",
                type: "POST",
                data: new FormData(this),
                contentType: false,
                cache: false,
                processData: false,
                beforeSend: function() {
                    //$("#preview").fadeOut();
                    $("#err").fadeOut();
                },
                success: function(data) {
                    $('#results').html(data);
                    
                }
            });
        });
        $('#form-data').submit(function() {
            return false;
        });
    });
    </script>
</body>

</html>

请告诉我我做错了什么。谢谢你的帮助

那么,我猜$u POST['password']没有设置:这是来自一个表单吗?如果我在设置$u POST['password']时回显其他内容,我会在那里得到回显的内容。但是当我尝试连接$GET值时,它不起作用。我只看到连接到valueWell的字符串,在这种情况下,可能没有设置GET值。这很难,你说的似乎有点矛盾,所以我们只能猜测,除非你提供具体的输入和输出数据。注意:不要忘记,这些变量不会在请求之间持续存在,如果你想每次读取一个变量,那么每次我用整个页面的图片和代码更新我的帖子时,你都必须在请求中发送它。谢谢。当您向change-password.php发出Ajax请求时,您没有在该请求中传递任何GET参数。在相反的场景中,当用户单击电子邮件中的链接时,它传递GET参数,但不传递POST参数。这就是为什么不能从if语句中读取值。如前所述,这些参数不会在请求之间持久化。如果需要,您需要在Ajax请求中再次发送相同的GET值。