PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 2, 2022

[FIXED] How to use an HTML button to trigger a function?

 July 02, 2022     html, mysql, php, xampp     No comments   

Issue

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);
?>

Solution

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>


Answered By - Fida Hasan
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing