按下按钮时如何获取提交类型按钮的 id (PHP + HTML)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13515496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:36:56  来源:igfitidea点击:

How to get id of submit type button, when button is pressed (PHP + HTML)

phphtml

提问by B.B10

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:

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

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

And I want to print the id of the button here. An suggestions? Thanks.

我想在这里打印按钮的 id。一个建议?谢谢。

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

回答by Ivan Pintar

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>

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

然后你可以用$_GET['id'].

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.

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

回答by Steven Moseley

The 'id' attribute doesn't submit to PHP, only the 'value' attribute submits.

'id' 属性不提交给 PHP,只有 'value' 属性提交。

What you should do is add a hidden input with the name "id", like this:

您应该做的是添加一个名为“id”的隐藏输入,如下所示:

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

回答by aleation

Use a hidden input field:

使用隐藏的输入字段:

<input type="hidden" value=".$row['I_ID']." name="input2"/>

Then access it in the second script through:

然后通过以下方式在第二个脚本中访问它:

$_POST['input2']; // or $_GET['input2'], depending on the method of the form submitting

回答by Sudar

As others have said, the id attribute is not passed to PHP.

正如其他人所说,id 属性不会传递给 PHP。

You can use the name attribute instead of id and it will get passed to PHP.

您可以使用 name 属性而不是 id ,它将被传递给 PHP。

If you have the following HTML

如果您有以下 HTML

<input type="submit" name="some_name" class="btn" value ="Add to cart">

The you can access it in PHP as

您可以在 PHP 中访问它

$_POST['some_name'] = "Add to Cart"

回答by kapantzak

You can grab the id with jquery and send it via ajax call:

您可以使用 jquery 获取 id 并通过 ajax 调用发送它:

$(document).on('click', 'input.btn', function() {
    var this_id = $(this).attr('id');
    ajaxCall(this_id);
});

function ajaxCall(this_id) {
    var data = 'id=' + this_id;

$.ajax({
    url: 'proccess.php',  
    type: "GET",    
    data: data,
    cache: false,
    success: function (html) {
            DO WHAT EVER YOU WANT WITH THE RETURNED html
    }         
});

Your 'proccess.php':

你的'proccess.php':

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