我有一個(gè)動(dòng)態(tài)表,它設(shè)置在 foreach 中,因此對(duì)于獲取的數(shù)組的每個(gè)項(xiàng)目創(chuàng)建一個(gè)新行。我在最后一列中每行都有一個(gè)按鈕。當(dāng)單擊該提交按鈕時(shí),我應(yīng)該會(huì)收到 PHP 中的 id。提交已正確完成,但我在 PHP 中收到錯(cuò)誤的 id。它基本上在提交時(shí)獲取數(shù)組的最后一個(gè) id。知道為什么嗎?這是表格:<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>當(dāng)我點(diǎn)擊see_rma這個(gè)時(shí),我收到的 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}如果我嘗試$cases["Dev_Id"];在表中的內(nèi)部循環(huán)中打印,它會(huì)打印得很好,因此它會(huì)正確打印每行的 Dev_Id。因此,這意味著數(shù)組或數(shù)據(jù)沒(méi)有任何問(wèn)題。我不知道為什么會(huì)發(fā)生這種情況,但這肯定是我第一次遇到這個(gè)問(wèn)題。我在許多其他表中都這樣做了,但由于某些原因,它在這個(gè)表中無(wú)法正常工作。phphtml數(shù)據(jù)表表單提交
1 回答

蠱毒傳說(shuō)
TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
您的表單中有多個(gè)<input>
同名元素,當(dāng)您提交表單時(shí),所有這些元素都會(huì)被提交,但 PHP 只能獲取其中之一。這就是為什么你最終只得到最后一個(gè)$_POST
。
看起來(lái)您應(yīng)該能夠通過(guò)將一些屬性從隱藏輸入移動(dòng)到按鈕(替換隱藏輸入)來(lái)解決此問(wèn)題。
<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>"> See RMA </button>
只有被點(diǎn)擊的按鈕才會(huì)被提交。請(qǐng)注意,更改按鈕的名稱后,您將不再需要see_rma
任何$_POST
名稱,因此,如果您有任何依賴于該名稱的代碼,則需要更改它以查找其他名稱。
- 1 回答
- 0 關(guān)注
- 109 瀏覽
添加回答
舉報(bào)
0/150
提交
取消