【问题标题】:Is it possible to store variable in input box in HTML or PHP是否可以在 HTML 或 PHP 的输入框中存储变量
【发布时间】:2021-08-01 13:59:27
【问题描述】:

我想创建一个只有可读性和书名的输入框,输入框可以存储变量吗?

P.S 我要改的是id,现在我成功禁用了输入框,但是输出的是$id而不是我使用$_GET方法的变量。

我的代码如下

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

<section class="signup-form">
  <h2>Pre-order</h2>
  <div class="signup-form-form">
    <form action="preorder.inc.php" method="post">
        <input type="text" disabled="disabled" name="id" value= $id >
      <input type="text" name="uid" placeholder="Username...">
      <input type="text" name="BRO" placeholder="BookRegistrationCode...">
      <button type="submit" name="submit">Pre-order</button>
    </form>
  </div>
  <?php
    // Error messages
     if (isset($_GET["error"])) {
      if ($_GET["error"] == "emptyinput") {
        echo "<p>Fill in all fields!</p>";
      }
      else if ($_GET["error"] == "wronginfo") {
        echo "<p>Wrong information!</p>";
      }
        else if ($_GET["error"] == "stmtfailed") {
        echo "<p>Something went wrong!</p>";
      }
      else if ($_GET["error"] == "none") {
        echo "<p>Success!</p>";
      }
     }
  ?>
</section>

【问题讨论】:

  • &lt;input type="text" disabled="disabled" name="id" value="&lt;?= $_GET['bookid']?&gt;"&gt;

标签: php html variables input


【解决方案1】:

&lt;?php echo $var; ?&gt; 放在 value 属性中。

&lt;input type="text" value="&lt;?php echo $var; ?&gt;" /&gt;

编辑

根据@arkascha 的评论,您可以使用替代的 php 短标签:

&lt;input type="text" value="&lt;?= $var; ?&gt;" /&gt;

根据文档:https://www.php.net/manual/en/language.basic-syntax.phptags.php

【讨论】:

猜你喜欢
  • 2010-10-08
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多