【问题标题】:Why do I can't get the title by using POST method?为什么我不能使用 POST 方法获取标题?
【发布时间】:2021-08-02 14:41:20
【问题描述】:

所以我试图通过在第一个 PHP 文件中使用 $_GET['title'] 从 URL 中获取标题,但我无法在第二个文件中获取该文件。

网址:

https://easy2book.000webhostapp.com/neworder.php?bookid=101&title=SENIOR%20secondary%20geography%20fieldwork%20and%20assessment%20practice%202021.%20For%20HKDSE%202021%20/%20Ip%20Kim%20Wai%20...%20[et%20al.].

第一个文件:

<?php
  include_once 'header.php';
    $id2 = mysqli_real_escape_string($conn, $_GET['bookid']);
    $title2 = mysqli_real_escape_string($conn, $_GET['title']);
?>

<section class="neworder-form">
  <h2>Order</h2>
  <div class="neworder-form-form">
    <form action="neworder.inc.php" method="post">
      <table>
            <tr>
              <td>Book ID:</td>
              <td>
                 <input type="text" disabled="disabled" name="bookid2" value="<?= $id2 ?>">
              </td>
            </tr>
           <tr>
              <td>Book Title: </td>
              <td>
                 <input type="text" disabled="disabled" name="title2" value="<?= $title2 ?>">
              </td>
           </tr>
           <tr>
               <td>Username: </td>
               <td>
                  <input type="text" name="uid2" placeholder="Username...">
               </td>
            </tr>
            <tr>
               <td>Comfirmed Book ID: </td>
               <td>
                  <input type="text" name="id2" placeholder="Please enter the Book ID....">
               </td>
            </tr>
        </table>
      <button type="submit" name="submit2">Order</button>
    </form>
  </div>
  <?php
    // Error messages
    if (isset($_GET["error"])) {
      if ($_GET["error"] == "emptyinput2") {
        echo "<p>Fill in all fields!</p>";
      }
      else if ($_GET["error"] == "usernametaken2") {
        echo "<p>Username already taken!</p>";
      }
    }
  ?>
</section>

第二个文件:

<?php

if (isset($_POST["submit2"])) {

  // First we get the form data from the URL
  $uid2 = $_POST["uid2"];
  $id2 = $_POST["id2"];
  $title2 = $_POST["title2"];

  // Then we run a bunch of error handlers to catch any user mistakes we can (you can add more than I did)
  // These functions can be found in functions.inc.php

  require_once "dbh.inc.php";
  require_once 'functions2.inc.php';

  // Left inputs empty
  // We set the functions "!== false" since "=== true" has a risk of giving us the wrong outcome
  if (emptyInputOrder2($uid2,$id2) !== false) {
    header("location: ../neworder.php?error=emptyinput&bookid=$id2&title=$title2");
        exit();
  }
    
  // Is the username exists
  if (uidExists2($conn, $uid2) !== true) {
    header("location: ../neworder.php?error=undefineuser");
        exit();
  }

  // If we get to here, it means there are no user errors

  // Now we insert the user into the database
    createUser($conn, $uid2, $id2);

} else {
    header("location: ../neworder.php");
    exit();
}

【问题讨论】:

  • $title2 = $_POST[""];?忘记写索引了?
  • @El_Vanja 已替换但仍未定义
  • 没有意义。 var_dump($_POST) 看看里面有什么。
  • @El_Vanja [uid2] => [id2] => [submit2] 它跳过了 bookid2 和 title2,可能是因为我使用了 value="= $title2 ?>" ?
  • 啊,我不敢相信我这么瞎……他们是残疾人。您可以直接在页面上打印值并使用隐藏的输入来进一步传输值,而不是使用禁用的输入。

标签: php html url post get


【解决方案1】:
  • 输入字段已禁用,禁用的输入不会发布。
  • 替换 $title2 = $_POST[""]; $title2 = $_POST["title2"];

【讨论】:

  • OP 正在发布这些值。第一个脚本从$_GET 获取值并将其放在文本输入的value 中。
  • 我的错,他应该填写:$title2 = $_POST[""]; $title2 = $_POST["title2"];并检查是否设置了值
  • @Firewizz 试图替换 $title2 = $_POST[""]; $title2 = $_POST["title2"];仍然未定义的索引:第 8 行 /storage/ssd1/923/16775923/public_html/neworder.inc.php 中的 title2
  • @WarrenChan 尝试将字段设置为只读而不是禁用。禁用的字段不会发布。 (正如 El_Vanja 提到的那样;))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 1970-01-01
  • 2011-09-17
  • 2014-10-27
  • 2022-01-12
  • 2022-01-05
  • 2014-03-01
相关资源
最近更新 更多