如何在我的表单中处理两个提交按钮

时间:2014-07-24 21:00:15

标签: php html forms

我正在尝试在表单中创建两个提交按钮

<form id="form-input-wrapper" action='test.php'>

//form items..
//form items..

    <button class="btn btn-primary" type="submit" value="old">first button.</button>

    <button class="btn btn-primary" type="submit" value="new">second button</button>
</form>

我的问题是如何区分用户在test.php页面中点击的按钮?

2 个答案:

答案 0 :(得分:2)

您需要将name属性添加到按钮

<button class="btn btn-primary" type="submit" name="old" value="old">first button.</button>

<button class="btn btn-primary" type="submit" name="new" value="new">second button</button>

要使用的php代码如下:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    //something posted

    if (isset($_POST['old'])) {
        //old
    } elseif (isset($_POST['new']){
        //new
    }
}

相关问题: How can I tell which button was clicked in a PHP form submit?

答案 1 :(得分:1)

使用按钮名称标签并在php中检查:

<button name="subject1" type="submit" value="HTML">HTML</button>
<button name="subject2" type="submit" value="CSS">CSS</button>

在php中:

<?php
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     if ( isset($_POST['subject1']) )   {
       // first button is clicked

     } elseif ( isset($_POST['subject2']) )   {
        //second button is clicked

     }
  }
?>