【问题标题】:Sample php self submitting form example [duplicate]示例php自我提交表单示例[重复]
【发布时间】:2014-01-18 07:17:31
【问题描述】:

您时不时需要一个工作示例,而我一直对 POST 和 GET 请求感到困惑。

【问题讨论】:

    标签: php forms post get


    【解决方案1】:

    $_POST 变量是在“单个页面”内从表单提交数据时使用的变量,而$_GET 变量是您可以“通过 URL 传递到另一个页面”的变量,从而启用其他 .php 页面通过$_GET 变量使用您的变量。

    还有$_REQUEST 可用于从表单中获取$_POST$_GET 变量的数据。

    【讨论】:

      【解决方案2】:
      <?php
      // Check if action is set
      if(isset($_POST["action"]))
      {
          switch($_POST["action"])
          {
              case "number_submit" :
                  // Submission from the number submit form
                  header("Location: ".$_SERVER["PHP_SELF"]."?number=".$_POST["number"]);
                  die();
              default :
                  die("Unknown action : ".$_POST["action"]);
                  break;
          }
      }
      ?>
      <html>
      <head>
          <title>Self Submit</title>
      </head>
      
      <body>
          <?php
          if(isset($_GET["number"]))
          {
              // Display the number if it is set.
              ?>
              Here is the number : <?php echo ($_GET["number"]); ?><br />
              <a href="<?php echo $_SERVER["PHP_SELF"]; ?>">Click here to enter another number..</a>
              <?php
          } else {
              // Display the form
              ?>
              <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
              <input type="hidden" name="action" value="number_submit" />
              Please enter a number : <input type="text" name="number" />
              <input type="submit" value="Submit" />
              </form>
              <?php
          }
          ?>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-21
        • 1970-01-01
        • 2014-03-02
        • 2016-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多