我有一个 php 页面,它从 mysql 数据库中获取数据作为 $certs[] 并将其填充为一个表: ($db 从文件中读取,它是常量)
<?php echo $cert["id"] ?>
<button type="button" class="awe" title="Edit" id="editbtn" onclick="openNewModalWithValue('Modal2')" value =<?php echo $cert["id"] ?> ></button>
<form action="sample.php" method="POST">
<input type="hidden" name="id" value=<?php echo $cert["id"] ?>>
<input type="hidden" name="db" value=<?php echo $db ?>>
<button type="submit" name="mission" title="Delete"></button>
</form>
id
值。 (就像按“编辑”按钮,你会得到设置了值的模态窗口。)所以我的模态窗口是:
<form id="modal-form" method="POST" action="sample2.php">
<input type="number" id="idb" name="idbase" />
<input type="text" id="nme" name="name"/>
<input type="hidden" name="db" value=<?php echo $db; ?>>
<input type="hidden" id= "idbs" name="idbs" />
<button id="form-submit" type="submit" >Edit</button>
</form>
function openNewModalWithValue(modal){
document.getElementById(modal).style.display = "block";
document.getElementById("idb").value = document.getElementById("editbtn").value;
}
问题是,即使 PHP 正确发送 $certs[] 值,并且表格正确填充,当我点击“编辑”按钮时,无论我在哪一行/数据中我得到了 PHP 数组中项目#1 的保留值点击。 除了对每一行应用计数器并对其进行操作之外,还有更简单的方法吗?
最佳答案
问题可能是 idb 是 DOM 中的多个 id。 document.getElementById 在整个页面中查找 Dom。所以你必须提供唯一的ID
关于javascript - HTML 元素在 PHP 数组中传递第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273062/