显示包含来自 PhP 表单的数据的 HTML 表格

我需要显示一个 HTML 表,其中包含我从 PhP 表单中获得的数据。我做了这样的事情,但它不能正常工作。它在列标题的顶部显示数据行。这是代码:


<html>

  <body>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

      Nome: <input type="text" name="nome">

      Ragione sociale: <input type="text" name="rag">

      Indirizzo: <input type="text" name="ind">

      Partita IVA: <input type="text" name="iva">

      <input type="submit">

    </form>


    <?php


    if ($_SERVER["REQUEST_METHOD"] == "POST") {

      $name = htmlspecialchars($_REQUEST['nome']);

      $rag = htmlspecialchars($_REQUEST['rag']);

      $ind = htmlspecialchars($_REQUEST['ind']);

      $iva = htmlspecialchars($_REQUEST['iva']);


      }


    ?> 


   <table>

    <tbody>

      <tr>

        <?php

        $a = array($name,$rag,$ind,$iva);

        for ($i=0; $i<count($a); $i++){

                print_r($a[$i]);

          echo "   ";

        }

        ?> 

      </tr>

      <tr>

        <td>|Nome|</td>

        <td>Ragione Sociale|</td>

        <td>Indirizzo|</td>

        <td>Partita IVA|</td>


      </tr>

     </tbody>

    </table>


  </body>

</html>


潇湘沐
浏览 81回答 1
1回答

叮当猫咪

您在第一行打印数据,而不输出任何<td>数据。然后在第二行打印你的标题/标题。像这样更改您的表格,看看这是不是您想要的:<table>&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>|Nome|</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Ragione Sociale|</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Indirizzo|</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Partita IVA|</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $a = array($name,$rag,$ind,$iva);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ($i=0; $i<count($a); $i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $a[$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody></table>有关表格的更多信息:https ://developer.mozilla.org/en-US/docs/Web/HTML/Element/table编辑:我还建议将您的表格放在里面,if ($_SERVER["REQUEST_METHOD"] == "POST") {这样它只会在您提交表格后打印出来
打开App,查看更多内容
随时随地看视频慕课网APP