【问题标题】:PHP - HTML form two possible actions?PHP - HTML 表单两种可能的操作?
【发布时间】:2010-01-18 12:49:51
【问题描述】:

我正在创建一个注册页面,我想让用户有机会查看他们的信息并返回并编辑它,然后单击将其插入数据库的确认按钮。

有没有办法包含两个指向不同脚本的提交按钮,或者我必须复制整个表单但使用隐藏字段来代替?

任何建议表示赞赏。

谢谢。

【问题讨论】:

    标签: php html forms


    【解决方案1】:

    您可以使用两个不同名称的提交按钮:

    <input type="submit" name="next" value="Next Step">
    <input type="submit" name="prev" value="Previous Step">
    

    然后检查哪个提交按钮被激活:

    if (isset($_POST['next'])) {
        // next step
    } else if (isset($_POST['prev'])) {
        // previous step
    }
    

    这是因为only the activated submit button is successful:

    • 如果一个表单包含多个提交按钮,只有激活的提交按钮是成功的。

    【讨论】:

      【解决方案2】:

      对于所有其他 HTML input 元素,您只需给它们一个名称和值对,以便它出现在 $_GET$_POST 中。这样,您可以根据按下的按钮进行条件检查。例如

      <form action="foo.php" method="post">
          <input type="text" name="input">
          <input type="submit" name="action" value="Add">
          <input type="submit" name="action" value="Edit">
      </form>
      

      $action = isset($_POST['action']) ? $_POST['action'] : null;
      if ($action == 'Add') {
          // Add button was pressed.
      } else if ($action == 'Edit') {
          // Edit button was pressed.
      }
      

      您甚至可以通过在数组中包含操作来进一步抽象它。

      【讨论】:

        【解决方案3】:

        你可以

        喜欢:

        http://sureshk37.wordpress.com/2007/12/07/how-to-use-two-submit-button-in-one-html-form/

        通过 php

        <!--    userpolicy.html      -->
        <html>
        <body>
        <form action="process.php" method="POST">
            Name <input type="text" name="username">
            Password <input type="password" name="password">
            <!--  User policy goes here           -->
        
            <!--            two submit button            -->
            <input type="submit" name="agree" value="Agree">
            <input type="submit" name="disagree" value="Disagree">
        </form>
        </body>
        </html>
        
        /*         Process.php         */
        <?php
        if($_POST['agree'] == 'Agree') {
            $username = $_POST['username'];
            $password = $_POST['password'];
              /* Database connection goes  here */
        
        }
        else {
            header("Location:http://user/home.html");
        }
        ?>
        

        或通过javascript

        【讨论】:

          【解决方案4】:

          我当然不会重复该表格,那将是不言而喻的 DRY 违规行为。大概每次提交表单时您都需要进行相同的数据检查,因此您可能只需要一个操作,并且仅在用户点击“批准”按钮时运行“添加到数据库”部分。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-11-01
            • 1970-01-01
            • 2011-06-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-21
            • 1970-01-01
            相关资源
            最近更新 更多