【发布时间】:2018-04-05 06:04:26
【问题描述】:
好吧,我真的被难住了。在 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 函数都是相同的,所以也许这就是问题所在,也许每个都应该是唯一的?
脚本:
<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>
【问题讨论】:
-
显示你的 JavaScript 代码
-
PHP 在服务器上执行一次。结果被传递到浏览器。如果您想使用 JavaScript 将行的值复制到弹出窗口中,即在 client 上,则必须从 PHP 生成的 HTML 代码中获取值。您不能再期望 PHP 命令/值做任何事情,因为它们不再存在于客户端上。除非你给每一行它自己的弹出窗口?无论如何,我们看不到你的代码。
-
@chris G 是的,每一行都有一个编辑按钮,它调用弹出的 html 表单
-
@datasci 我明白了。你能展示更多你的代码吗?喜欢你的整个while循环吗?你的 JavaScript 代码呢?
-
@datasci 你为什么一直说我从来没有发表过我的第一条评论?这是我从一开始就建议的。
标签: javascript php html mysql sql