【问题标题】:How to use an HTML button to trigger a function?如何使用 HTML 按钮触发功能?
【发布时间】:2019-11-13 14:12:30
【问题描述】:

我正在用来自数据库的信息填充一个 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);
?>

【问题讨论】:

  • 你能解释一下你想让按钮做什么吗?
  • 使用我现在拥有的代码,表格会自动填写,但我希望它在我点击按钮后填写
  • 我认为您只是在寻找 onclick 事件。
  • 是否可以重新加载页面,或者您是否尝试在不重新加载的情况下这样做?如果您不想重新加载页面,则需要使用 AJAX。
  • 我不介意它是否会重新加载,但如果不是太麻烦的话,我想知道如何双向进行

标签: php html mysql xampp


【解决方案1】:

你需要使用jQuery(最简单的ajax使用方式)

看看stackoverflow上的这个问题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>

【讨论】:

  • 谢谢,我现在知道了。还有一个问题。有没有办法让它在页面上加载而不是作为警报弹出?
猜你喜欢
  • 2020-02-19
  • 2020-11-13
  • 1970-01-01
  • 2021-02-27
  • 2013-12-10
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
  • 2022-12-12
相关资源
最近更新 更多