简体   繁体   English

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

[英]How to handle two submit buttons in my form

I am trying to create two submit button in my form 我想在我的表单中创建两个提交按钮

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

My question is how to distinquish which button the user clicks in my test.php page? 我的问题是如何区分用户在test.php页面中点击的按钮?

You need to add the name attribute to your buttons 您需要将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>

The php code to use would look like: 要使用的PHP代码如下所示:

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

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

related question: How can I tell which button was clicked in a PHP form submit? 相关问题: 如何判断在PHP表单中单击了哪个按钮?

Use button name tag and check them in php: 使用按钮名称标签并在php中检查它们:

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

And in php: 在PHP中:

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

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

     }
  }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM