dslfq06464 2018-05-25 11:53 采纳率: 0%
浏览 112
已采纳

PHP表单数据检查提交按钮名称

I have two forms on the same page, each with different fields and different submit button names.

<button type="submit" name="formOne">Submit form</button>
<button type="submit" name="formTwo">Submit form</button>

On my mailer.php page I would like to check which submit button has been clicked, and process the code accordingly.

The code I have so far works for a single form, as follows;

if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
    // my form 1 data
    // do stuff
}

However I thought I coulld add an if/else to check which submit button was clicked, something like;

if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
    if($_POST['formOne']){
        // form 1 data
        // my form 1 data
        // do stuff
    }
    if($_POST['formTwo']){
        // form 2 data
        // my form 2 data
        // do stuff
    }
}

This doesn't seem to work. The error I receive is;

Notice: Undefined index: formOnein ... line 7

How can I achieve this?

  • 写回答

3条回答 默认 最新

  • douzhang6646 2018-05-25 12:07
    关注

    If you want, you can alternatively use hidden inputs. You can access hidden inputs from php just like a normal text input.

    <form method="GET" action="https://postman-echo.com/get">
        <input type="hidden" name="form" value="one" />
        <button type="submit">Submit form</button>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?