首页 > 解决方案 > 如何使用 HTML 按钮触发功能?

问题描述

我正在使用来自数据库的信息填充 HTML 表,并希望使用按钮触发它。

有人可以帮我解决这个问题,也许可以通过示例添加一些指向相关网站的链接吗?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div method="GET">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nombre</th>
                    <th>Usuario</th>
                </tr>
                <?php
                include "php/populate.php";
                ?>

            </thead>
        </table>
        <input type="button" value="button" id="button">
    </div>
</body>
</html>
<?php 
$result = mysqli_query($enlace,"SELECT * FROM tb_personas");

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['ID'] . "</td>";
    echo "<td>" . $row['txt_nombre'] . "</td>";
    echo "<td>" . $row['txt_usuario'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($enlace);
?>

标签: phphtmlmysqlxampp

解决方案


您需要使用 jQuery(使用 ajax 的最简单方法)

在stackoverflow上查看this question how to call a php script on a html button click

此外,您将数据包含在表的标题中,而不是它们应该包含在表的正文中。

   <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Nombre</th>
                <th>Usuario</th>
            </tr>                
        </thead>

        <tbody>
            <?php   include "php/populate.php";       ?>
        </tbody>


    </table>

推荐阅读