数组中提交按钮的名称,并标识单击按钮的索引


Name of submit buttons in array and identifying the index of clicked button

根据搜索到的查询,将为我的html页面动态创建多个提交按钮。当单击某个特定按钮时,它将打开与其他按钮相同的页面。所以我给提交按钮名称分配了一个数组。我需要确定使用php点击哪个提交按钮。基本上,我想找到点击提交按钮的数组索引。

您可以使用隐藏输入来跟踪单击的提交按钮。

// First create an HTML hidden input
<input id="the_clicked_id" type="hidden" name="the_clicked_id" value=""/>
// It's a bit ugly, but all your submits would need an onclick event
// and an ID. They are easily added with PHP tho
<?php
    for ($i = 0; $i < $some_length; $i++) {
        echo "<input id='"submit_$i'" name='"submit_$i'" type='"submit'" value='"Submit'" onclick='"return myId(this);'"/>" . PHP_EOL;
    }
?>
// Javascript side
myId = function (element) {
    document.getElementById('the_clicked_id').value = element.id;
    return true;
}

然后,您可以使用访问这个新值(按住单击的提交按钮id)PHP端

$clicked_id = $_POST['the_clicked_id'];
// or
$clicked_id = $_GET['the_clicked_id'];

如果你需要我编辑答案,请告诉我。