PHP 不会回显用户输入

PHP won't echo user input

对此有点陌生,所以如果我的问题经常被问到或者没有任何意义,我很抱歉。我试图让 PHP 将用户的输入回显到屏幕上的表单中。每当我测试表格时,都没有任何变化。您将如何处理这个问题?

 <body>
    <script src="index.js"></script>
  </body>
</html>

<center><form name="name" method="get" class="form" id="nameInput">
  <center><h4>Name</h4></class></center>
  <center><input>
  
<center><div><form name="post" method="get" class="form" id="postInput">
  <center><h4>Post</h4>
  <center><input></div>
  <center><div><button id="postButton">Post</button></div>

<?php 
if(empty($_GET['name']) && empty($_GET['post'])){
  echo "<h3>test</h3>";
  $name = $_GET['name'];
  $post = $_GET['post'];
 

}else{
  $name = $_GET['name'];
  $post = $_GET['post'];
  echo "<h3> User: $name </h3>";
  echo "<h3> Post: $post </h3>";
}

?>

类似这样的内容可能会帮助您入门:

<form method="get" class="form">
    <center>
        <strong>Name</strong>
        <input type="text" name="name" value="" />
    </center>
    <center>
        <strong>Post</strong>
        <input type="text" name="post" value="" />
    </center>
    <center>
        <input type="submit" value="Go!" />
    </center>
</form>

试试这个:-

<body>
  <center>
    <form method="get" class="form" id="nameInput">
      <h4>Name</h4>
      <input name="Name" type="name">
      <h4>Post</h4>
      <input name="Post type="textarea">
      <br><input type="submit" name="sub">
    </form>
  </center>

  <?php
  if(isset($_GET['sub'])) {
    if(empty($_GET['Name']) && empty($_GET['Post'])) {
      echo '<h2>Name: '.$_GET['Name'].'</h2>';
      echo '<h2>Post: '.$_GET['Post'].'</h2>';
    }
  }
  ?>
<script src="Index.js"></script>
</body>
</html>

错误是您将名称属性赋予表单 element.Each 输入应该有自己的名称,而不是 form.And 尝试将 post 和名称输入保持在一个表格.

希望对您有所帮助。

首先,主要问题是由您的HTML代码引起的。

Mozilla 说;

The <form> HTML element represents a document section containing interactive controls for submitting information .

因此,您需要所有 input / textarea 等元素都应该在一个表单中。为了做到这一点并使其更简单,我写了下面的代码片段。这在这个例子中会更有意义;

<form method='GET'>
    <!-- INPUT Start -->
    <input type="text" name='name'>
    <input type="text" name='post'>
    <!-- INPUT End -->

    <!-- SEND BUTTON Start -->
    <input type="submit" value='send'>
    <!-- SEND BUTTON End -->
</form>

<form> HTML 元素的另一个重要属性是 action='where_are_you_going_path' 但现在这是不必要的,因为您想重定向您形成的同一页面。

其次,您在打开页面时遇到一些 PHP 错误;

A PHP Error was encountered Severity: Notice

Message: Undefined index: name

..

发生这种情况是因为您错误地检查了 $_GET 数组。因为你无法检查它是否为空或没有到一个尚未分配的索引。

要处理这个问题,您可以尝试isset()方法。

<?php 
    if(isset($_GET['name']) && isset($_GET['post'])){
      echo "<h3> User:".$_GET['name']." </h3>";
      echo "<h3> Post: ".$_GET['post']." </h3>";
    }
?>

我想亲切地提出这个建议;在跳转到 PHP 之后,使用 Mozilla Documentation. 学习 HTML / CSS 基础知识。这对你来说会更容易。