简体   繁体   English

是否可以在 HTML 或 PHP 的输入框中存储变量

[英]Is it possible to store variable in input box in HTML or PHP

I would like to create an input box that is only readable and have the title of the book, is it possible that the input box can store a variable?我想创建一个只有可读且有书名的输入框,输入框可以存储变量吗?

PS The one I want to change is the id, now I successfully disabled the input box, but the output is $id instead of the variable that I use the $_GET method. PS我要更改的是id,现在我成功禁用了输入框,但是output是$id而不是我使用$_GET方法的变量。

My code is as follow我的代码如下

<?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>

Put <?php echo $var; ?><?php echo $var; ?> <?php echo $var; ?> inside the value attribute. <?php echo $var; ?>在 value 属性中。

<input type="text" value="<?php echo $var; ?>" />

EDIT编辑

As per @arkascha's comment, you can use alternative php short tags:根据@arkascha 的评论,您可以使用替代的 php 短标签:

<input type="text" value="<?= $var; ?>" />

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM