【问题标题】:PHP won't echo user inputPHP 不会回显用户输入
【发布时间】:2021-10-05 16:24:09
【问题描述】:

对此有点新意,所以如果我的问题经常被问到或者没有任何意义,我很抱歉。我试图让 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>";
}

?>

【问题讨论】:

  • 你可能想看看在某个时候添加一些&lt;/form&gt;标签????
  • 你应该退后一步,先学习如何正确编写 HTML,你缺少结束标签、输入名称、按钮类型等。

标签: php html forms get


【解决方案1】:

试试这个:-

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

错误是您为表单元素赋予了 name 属性。每个输入都应该有自己的名称,而不是表单。并尝试将 post 和 name 输入保持在一个表单中。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这样的事情可能会帮助您入门:

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

    【讨论】:

      【解决方案3】:

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

      Mozilla 说;

      &lt;form&gt; HTML 元素表示一个文档部分,其中包含 用于提交信息的交互式控件。

      因此,您需要将所有input / textarea 等元素放在一个表单中。为此并使其更简单,我在下面编写了代码 sn-p。这在这个例子中会更有意义;

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

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

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

      遇到 PHP 错误 严重性:通知

      消息:未定义的索引:名称

      ..

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2014-01-09
        • 2016-09-28
        • 1970-01-01
        • 2013-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多