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/

相关文章:

php - 如何使用 symfony 1.4 从模板中获取 DQL 结果?

php - Google 云消息 GCM - 未发送推送通知(服务器端)

php - 包含在 PHP 中是无效的

php - 如何注册命名空间的自定义 View 助手?

Javascript 和 tipped 数组

html - CSS 和 HTML : HTML code wont take the style from the CSS

javascript - 如何使用 Jquery 将值附加到嵌套 div

javascript - react js : import component to route

javascript - jQueryparents() 和parent().parent() 只能看到上一级?

javascript - 如何通过单击按钮调用此脚本