【问题标题】:the server request method prints GET even though the form method is POST即使表单方法是 POST,服务器请求方法也会打印 GET
【发布时间】:2021-04-11 00:11:00
【问题描述】:

我在下面写了这段代码,当我提交表单时,$_SERVER["REQUEST_METHOD"] 会打印 GET 而不是 POST,即使表单方法 = "POST"

<?php
    include "init.php";
    include $temp . "navbar.php";
    echo $_SERVER["REQUEST_METHOD"];
    if($_SERVER["REQUEST_METHOD"]=='POST'){
        echo $_POST["name"];
    }
?>
<input type="hidden" value="admin" class="title">
<form action="index.php" method="POST">
    <input type="text" name="name">
    <input type="submit" value="ok">
</form>
<?php
    include $temp . "foot.php";
?>

【问题讨论】:

  • 正确,因为你使用了GET方法访问页面
  • 当你提交表格时它应该变成POST?当然是GET当你第一次访问页面时
  • 尽管我怀疑 Areg 是对的,但您是如何访问该页面的?
  • 如果你提交然后你提出POST请求

标签: php post


【解决方案1】:

您可以简单地检查$_POST['name'] 是否已设置:

if(isset($_POST['name'])) {
    // ...
}

由于您发布到同一页面,因此您不需要在表单中包含 action 属性。

<?php
    include "init.php";
    include $temp . "navbar.php";
    if(isset($_POST['name']) {
        echo $_POST['name'];
    }
?>
<input type="hidden" value="admin" class="title">
<form method="POST">
    <input type="text" name="name" value="" />
    <input type="submit" value="ok">
</form>
<?php
    include $temp . "foot.php";
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多