javascript - 使用 php 将 sql 转为 html

标签 javascript php html mysql sql

好吧,我真的被难住了。在 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 函数都是相同的,所以也许这就是问题所在,也许每个按钮都应该是唯一的?

我在此处附加了一张图像,以供编辑按钮和弹出表单引用。 enter image description here

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/

相关文章:

javascript - 我需要将数据添加到 FabricJS 对象并在导出 Canvas 时使用它们

php - 单击按钮在表中插入值

html - 背景大小 : cover stretching on certain phones

javascript - Javascript:如何检查一个数组是否恰好包含另一个数组?

javascript - Angular $http 在 404 上调用成功

javascript - 为什么 Angular $routeProvider 配置需要在 HTML 中声明的 ngView

php - Web开发中的实现问题

php - if else 条件不适用于 php 中的数组值

html - Bootstrap 媒体查询不起作用

javascript - 如何捕获远程加载数据的点击事件并用 angularJS 显示