在数据表中提交此按钮时未提交正确的行 ID
When submitting this button inside a datatable doesnt submit the right row id
我有一个动态 table 设置在 foreach 中,因此对于获取的数组的每个项目创建一个新行。我在最后一列中每行都有一个按钮。单击该提交按钮时,我想在 PHP 中收到该按钮的 ID。提交已正确完成,但我在 PHP 中收到错误的 ID。它基本上在提交时采用数组的最后一个 id。知道为什么吗?
这里是table:
<form method="post" id="frm-example" action="<?php echo $_SERVER["PHP_SELF"] . '?' . e(http_build_query($_GET)); ?>">
<table id="example" class="display compact">
<thead>
<th>Device</th>
<th>Sales date</th>
<th>Client comments</th>
<th>Breakage count</th>
</thead>
<tbody>
<?php foreach ($arr_cases_devices as $cases) { ?>
<tr>
<td>
<?php echo $cases['name']; ?>
</td>
<td>
<?php echo $cases["sales_date"]; ?>
</td>
<td>
<?php echo $cases["dev_comment"]; ?>
</td>
<td>
<input type="hidden" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>" />
<button type="submit" name="see_rma">See RMA</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
单击 see_rma
时,这是我在 PHP 中收到的内容:
if (isset($_POST['see_rma'])) {
$selected_dev = e($_POST['device_id_breakage']);
print_r($selected_dev); // prints the "Dev_Id" of the last row, not of the row clicked
}
如果我尝试在 table 的内部循环中打印 $cases["Dev_Id"];
,它打印得非常好,所以它正确地打印了每一行的 Dev_Id。所以,这意味着数组或数据没有任何问题。我不知道为什么会这样,但这肯定是我第一次遇到这个问题。
我在许多其他 table 中都这样做,但由于某些原因,它无法正常工作。
您的表单中有多个同名的<input>
元素,提交表单时将全部提交,但PHP只能获取其中一个。这就是为什么你最终只得到 $_POST
.
中的最后一个
看起来你应该能够通过将一些属性从隐藏输入移动到按钮(替换隐藏输入)来解决这个问题。
<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>">
See RMA
</button>
只有被点击的按钮才会被提交。请注意,更改按钮的名称后,$_POST
中将不再有 see_rma
,因此如果您有任何依赖于此的代码,则需要更改它以查找取而代之的是其他名称。
我有一个动态 table 设置在 foreach 中,因此对于获取的数组的每个项目创建一个新行。我在最后一列中每行都有一个按钮。单击该提交按钮时,我想在 PHP 中收到该按钮的 ID。提交已正确完成,但我在 PHP 中收到错误的 ID。它基本上在提交时采用数组的最后一个 id。知道为什么吗?
这里是table:
<form method="post" id="frm-example" action="<?php echo $_SERVER["PHP_SELF"] . '?' . e(http_build_query($_GET)); ?>">
<table id="example" class="display compact">
<thead>
<th>Device</th>
<th>Sales date</th>
<th>Client comments</th>
<th>Breakage count</th>
</thead>
<tbody>
<?php foreach ($arr_cases_devices as $cases) { ?>
<tr>
<td>
<?php echo $cases['name']; ?>
</td>
<td>
<?php echo $cases["sales_date"]; ?>
</td>
<td>
<?php echo $cases["dev_comment"]; ?>
</td>
<td>
<input type="hidden" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>" />
<button type="submit" name="see_rma">See RMA</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
单击 see_rma
时,这是我在 PHP 中收到的内容:
if (isset($_POST['see_rma'])) {
$selected_dev = e($_POST['device_id_breakage']);
print_r($selected_dev); // prints the "Dev_Id" of the last row, not of the row clicked
}
如果我尝试在 table 的内部循环中打印 $cases["Dev_Id"];
,它打印得非常好,所以它正确地打印了每一行的 Dev_Id。所以,这意味着数组或数据没有任何问题。我不知道为什么会这样,但这肯定是我第一次遇到这个问题。
我在许多其他 table 中都这样做,但由于某些原因,它无法正常工作。
您的表单中有多个同名的<input>
元素,提交表单时将全部提交,但PHP只能获取其中一个。这就是为什么你最终只得到 $_POST
.
看起来你应该能够通过将一些属性从隐藏输入移动到按钮(替换隐藏输入)来解决这个问题。
<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>">
See RMA
</button>
只有被点击的按钮才会被提交。请注意,更改按钮的名称后,$_POST
中将不再有 see_rma
,因此如果您有任何依赖于此的代码,则需要更改它以查找取而代之的是其他名称。