dstobkpm908182 2016-06-12 13:46
浏览 52

如何用$ message替换整个表单?

I am new in php, so I need your help. At first I created two .php files. The first included a form and the second included the message.

I wish to replace the form to a message in the same page after submit. how I do that?

<?php
  $message="";
  if (isset($_POST["submitButton"])) {
    $name= $_POST["name"];
    $age= $_POST["age"];
    $username= $_POST["username"];
    $message= "your name is ". $name. ",you are ".$age
    ." and your reddit's username is ".$username;
  }
 ?>

<html>
  <head>
    <meta charset="utf-8">
    <title>new file</title>
  </head>
  <body>
    <?php echo $message; ?>
    <form  action="" method="post">
    <table >
      <tr>
        <th>name:</th>
        <td>
          <input type="text" name="name" >
        </td>
      </tr>
      <tr>
        <th>age:</th>
        <td>
          <input type="text" name="age" >
        </td>
      </tr>
      <tr>
        <th>username:</th>
        <td>
          <input type="text" name="username">
        </td>
      </tr>
    </table>
    <input type="submit"  value="click here for save" name="submitButton">
  </form>
  </body>
</html>
  • 写回答

2条回答 默认 最新

  • dongxing1960 2016-06-12 13:50
    关注

    Put the form in a conditional.

    <?php
      $message= isset($_POST["submitButton"]) ? "your name is ". $_POST["name"]. ",you are ".$_POST["age"]
        ." and your reddit's username is ".$_POST["username"] : "";
     ?>
    
    <html>
      <head>
        <meta charset="utf-8">
        <title>new file</title>
      </head>
      <body>
        <?php if (!empty($message)) {
             echo $message; 
         } else { ?>
        <form  action="" method="post">
        <table >
          <tr>
            <th>name:</th>
            <td>
              <input type="text" name="name" >
            </td>
          </tr>
          <tr>
            <th>age:</th>
            <td>
              <input type="text" name="age" >
            </td>
          </tr>
          <tr>
            <th>username:</th>
            <td>
              <input type="text" name="username">
            </td>
          </tr>
        </table>
        <input type="submit"  value="click here for save" name="submitButton">
      </form>
        <?php } ?>
      </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线