0

I am populating an HTML table with information from a database and want to trigger it with a button.

Can someone help me with this, and perhaps add some links to relevant website with examples?

<!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);
?>
5
  • Can you explain a bit more what you want the button to do? Commented Jul 2, 2019 at 22:38
  • with the code i have now the table fills in automaticly but i want it to fill in after i click the button Commented Jul 2, 2019 at 22:40
  • I think you're just looking for the onclick event.
    – caden311
    Commented Jul 2, 2019 at 22:44
  • Is it okay if the page reloads, or are you trying to do it without reloading? You'll need to use AJAX if you don't want the page to reload. Commented Jul 2, 2019 at 22:46
  • i wouldn't mind if it reloads but would like to know how to do it both ways if it isn't too much trouble Commented Jul 2, 2019 at 22:52

1 Answer 1

0

You need to use jQuery (the easiest way to use ajax)

take a look at this question on stackoverflow how to call a php script on a html button click

additionally you are including your data at table's header, instead they should be included in table's body.

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

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


    </table>
1
  • Thank you i got it now. just one more question. is there a way to make it load on the page instead of popping up as an alert ? Commented Jul 2, 2019 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.