javascript - HTML 元素在 PHP 数组中传递第一个值

标签 javascript php html

我有一个 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"] ?> >&#xf019</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">&#xf00d</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>

   	   
用于打开模态窗口的 Javascript 是:

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/

相关文章:

php - 在 php 中使用 pdo 更新不起作用

html - 单击时,使 <li> 项变为粗体

javascript - 了解最终用户在打开模式窗口时单击了哪个 div

javascript - d3.js 中对数刻度的奇怪行为

javascript - 使用 AdWords 脚本/Javascript 在电子表格的不同行上打印数组中的对象

php - 使用数据库中的下拉列表根据第一个下拉列表的选择设置另一个下拉列表的值

php - Laravel 4/5 搜索表单,如

html - "Correct"在 HTML 中标记推荐的方法

javascript - 如何使用 Javascript 了解父/容器样式?

javascript - 根据值过滤对象数组