Two submit buttons in one form

前端 未结 19 2058
不思量自难忘°
不思量自难忘° 2020-11-21 23:23

I have two submit buttons in a form. How do I determine which one was hit serverside?

19条回答
  •  无人及你
    2020-11-22 00:17

    You can also do it like this (I think it's very convenient if you have N inputs).

    
    
    
    

    A common use case would be using different ids from a database for each button, so you could later know in the server wich row was clicked.

    In the server side (PHP in this example) you can read "row" as an array to get the id. $_POST['row'] will be an array with just one element, in the form [ id => value ] (for example: [ '123' => 'something' ]).

    So, in order to get the clicked id, you do:

    $index = key($_POST['row']);
    

    http://php.net/manual/en/function.key.php

提交回复
热议问题