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

时间:2016-06-12 13:46:45

标签: php

我是php新手,所以我需要你的帮助。 起初我创建了两个.php文件。第一个包含表单,第二个包含消息。

我希望在提交后将表单替换为同一页面中的消息。 我是怎么做到的?

<?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 个答案:

答案 0 :(得分:0)

form置于条件中。

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

答案 1 :(得分:0)

您可以使用以下给定的代码。 empty()函数确定变量是否被视为空。如果变量不存在或者其值等于FALSE,则该变量被视为空。如果变量不存在,则empty()不会生成警告。

通过在条件下使用它可以解决您的问题。

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