首页 > 解决方案 > HTML 表单提交后的 POST 数据为空,但请求类型为 POST

问题描述

所以,我正在尝试将一些数据从 HTML 表单发布到 PHP 脚本。这是我正在使用的表格:

<form style="border: 1px solid black; border-radius: 5px; padding: 10px;" action="order2.php" method="post">
  <div class="form-group">
    <label>Service Type</label>
    <select class="form-control" id="service">
      <?php
        $sql = "SELECT name FROM services ORDER BY name ASC";
          $statement = $mysqli->prepare($sql);
          $statement->execute();
          $statement->store_result();

          $result = get_result($statement);

          while($data = array_shift($result)) {
            echo('<option>'.$data['name'].'</option>');
          }
      ?>
    </select>
    <small id="serviceHelp" class="form-text text-muted"></small>
  </div>

  <div class="form-group">
    <label>Amount</label>
    <input type="number" class="form-control" id="amount" placeholder="1000">
    <small id="priceHelp" class="form-text text-muted"></small>
  </div>

  <div class="form-group">
    <label>Link</label>
    <input type="text" class="form-control" id="link" placeholder="">
    <small id="linkHelp" class="form-text text-muted"><strong>Warning</strong>! Please keep to the format of the example provided!</small>
  </div>

  <div class="form-group">
    <label>e*pvp Username</label>
    <input type="text" class="form-control" id="username" placeholder="i0N">
    <small id="linkHelp" class="form-text text-muted"><strong>Warning</strong>! Please make sure your username is correct (copy & paste it), because the payment will fail otherwise!</small>
  </div>

  <div style="text-align: center;"><button type="submit" class="btn btn-danger" style="padding-left: 30px; padding-right: 30px;">Submit</button></div>
</form>

正如您在最顶部看到的那样,“方法”设置为“发布”。表单成功地重定向了我,当我通过检查器查看请求时,它还向我显示发出了 POST 请求。但是,没有任何数据通过。如果我尝试var_dump($_POST);,它也只会显示array(0) { }。我错过了什么?

提交表单时,我的 error_log 中也永久出现此 PHP 错误:

[30-Jul-2018 04:14:28 America/New_York] PHP Notice:  Undefined index: service in /home/maxkygbx/public_html/epvpsocial/order2.php on line 6
[30-Jul-2018 04:14:28 America/New_York] PHP Notice:  Undefined index: service in /home/maxkygbx/public_html/epvpsocial/order2.php on line 7

这是相应的行:

<?php
  if(is_null($_POST['service']) || $_POST['service'] == ""){
    echo($_POST['service']);
    die();
  }

标签: phphtml

解决方案


您的输入字段没有任何“名称”属性。

您的输入必须如下所示:

<input type="number" name="amount" class="form-control" id="amount" placeholder="1000">

推荐阅读