按下按钮时如何获取提交类型按钮的ID

按下按钮时如何获取提交类型按钮的ID

问题描述:

我想打印出我按下的按钮的ID.为表中的每个按钮动态设置ID.这是我的HTML代码:

I want to print out the id of the button I pressed. The id is set dynamically for each button in a table. This is my HTML code:

    echo '<td><center><input type="submit" id="'.$row['I_ID'].'" class="btn"
name="Add" value ="Add to cart"><center></td><tr>';

我想在这里打印按钮的ID.

And I want to print the id of the button here.

if (isset($_POST['Add'])) {
    $ID = $_GET['id'];
    echo $ID;
    echo '<br/>' . "* The item has been added to your cart.";
}

如果您不想使用隐藏字段,则可以这样设置提交按钮:

If you do not wish to use a hidden field, you could set your submit button like this:

<button type="submit" name="id" value="value">Submit</button>

然后您可以使用 $ _ GET ['id'] 捕获它.

Then you can catch it with $_GET['id'].

您可以有许多具有相同名称( id )的 type ="submit" 按钮,其值将是被单击的值,或者是第一个一种,如果表单是通过按Enter提交的.

You can have many buttons of type="submit" with the same name (id), and the value will be the one which was clicked, or the first one, if the form was submitted by pressing enter.