好吧,我真的被难住了。在 screen.php 上,我显示了数据库表,并且为创建的每个数据库行生成了一个编辑按钮。编辑按钮调用 JavaScript 函数,该函数打开一个弹出 html 表单。
对于数据库中的每一行,这是在 html 中生成新行的 PHP 代码:
$output .= "<tr><td>". $row["name"]. "</td><td>". $row["country"]. "</td><td>". $row["base_nav"]."</td><td>"."<button type='button' rel='tooltip' title='Remove' class='btn btn-danger btn-simple btn-xs'><i class='fa fa-times'></i></button><button onclick='edit();' type='button' rel='tooltip' title='Edit' class='btn btn-warning btn-simple btn-xs'><i class='fa fa-edit'></i></button>"."</td></tr>";
当用户单击编辑按钮时,该特定行应使用该行中的数据填充弹出框。
这是我在 screen.php 文件中的代码。
<?php
require 'login_crudentials.php';
$connection = new mysqli($host, $user, $pword, $database, 3306);
if ($connection ->connect_error) die($connection ->connect_error);
$query = "select * from base";
$result = $connection->query($query);
while($row = $result->fetch_assoc())?>
在弹出的 html 表单中,在 value 参数下我有例如:
value="<?php echo $row["name"]; ?>">
表格完全空白。数据不会被复制。黄色按钮是触发弹出窗口的编辑按钮。不过,对于生成的所有编辑按钮来说,javascript 函数都是相同的,所以也许这就是问题所在,也许每个按钮都应该是唯一的?
j脚本:
<script>
// Get the modal
var editbasemodal = document.getElementById('editbasemodal');
// Get the <span> element that closes the modal
var editbasespan = document.getElementById("editbaseclose");
// When the user clicks the button, open the modal
function editbase(){
editbasemodal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
editbasespan.onclick = function() {
editbasemodal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.addEventListener("click", function(event){
if (event.target == editbasemodal) {
editbasemodal.style.display = "none";
}
});
</script>
最佳答案
document.querySelector('#editbasemodal input[name=country]').value = elementText;
关于javascript - 使用 php 将 sql 转为 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665181/