在一个表单上多次提交

时间:2013-09-02 14:46:41

标签: php forms

我有两个注册表格按钮。

<input type="submit" name="submit1" value="Pay Now" class="submit" id="submit1" />
<input type="submit" name="submit2" value="Pay Later" class="submit" id="submit2" />

检查是否按下任一按钮

if((isset($_POST['submit1'])) or (isset($_POST['submit2'])))

然后PHP代码清理和验证任一输入的数据

现在我希望“立即付款”转到一个页面,“稍后付款”转到另一个页面,但我无法弄明白。感谢

4 个答案:

答案 0 :(得分:2)

您可以使用$_SESSION。设置会话后,重定向页面并获取会话数据

if(isset($_POST['submit1']) || isset($_POST['submit2'])) {
    $_SESSION['post'] = $_POST;

    if($_POST['submit1'])
        header("Location: pay_now.php");
    elseif($_POST['submit2'])
        header("Location: pay_later.php");
}

pay_now.php或pay_later.php

$data = $_SESSION['post'];

答案 1 :(得分:2)

使用这样的表单操作:

<form action="" method="post">

<input type="submit" name="submit1" value="Pay Now" class="submit" id="submit1" onclick="this.form.action='page1.php'" />
<input type="submit" name="submit2" value="Pay Later" class="submit" id="submit2" onclick="this.form.action='page2.php'" />

</form>

答案 2 :(得分:0)

试试这个

if(isset($_POST['submit']))
{
  if($_POST['submit'] == 'Pay Now')
  {
   echo $_POST['submit'];

  }
  if($_POST['submit'] == 'Pay Later')
  {
   echo  $_POST['submit']
  }
}

答案 3 :(得分:0)

您可以在PHP中执行此操作,但您也可以使用onlick事件在JS中执行此操作。

<form method="POST" action="" name="dynamicForm">
   <input type="button" name="submit" value="Pay Now" class="submit" id="submit1" onclick="buttonClicked(1);" />
   <input type="button" name="submit" value="Pay Later" class="submit" id="submit2" onclick="buttonClicked(2);" />
</form>

<script type="text/javascript">
    function buttonClicked(type) {
        if (type === 1) {
           document.dynamicForm.action = 'firstUrl';
        } else {
           document.dynamicForm.action = 'secondUrl';
        }
        document.dynamicForm.submit();
    }
</script>