Home » Php » How to display a php result from database in HTML table on the same webpage (in one script)

How to display a php result from database in HTML table on the same webpage (in one script)

Posted by: admin October 21, 2018 Leave a comment

Questions:

Really quick question. How can I select a row from my database using PHP and display it in a HTML table below my current HTML code? As you can see I have attempted to call the DisplayTable () function when I press an HTML button which is in the same script. However, the function is not being called. I would really appreciate some help. Thanks!

<!DOCTYPE html>

<html>
<head>
<title align = "center"> User Info Panel </title>
</head>

<style>
body {
  background: #eee;
}

h1 {
  font-family: "Roboto", sans-serif;
}

form {
  margin: 150px auto;
  position: relative;
  z-index: 1;
  background: #FFFFFF;
  max-width: 360px;
  padding: 45px;
  text-align: center;
  box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0,           0.24);
 }
 form input {
 font-family: "Roboto", sans-serif;
outline: 0;
background: #f2f2f2;
width: 100%;
border: 0;
margin: 0 0 15px;
padding: 15px;
box-sizing: border-box;
font-size: 14px;
}

#button {
font-family: "Roboto", sans-serif;
text-transform: uppercase;
outline: 0;
background: #4CAF50;
width: 100%;
border: 0;
padding: 15px;
color: #003366;
font-size: 14px;
-webkit-transition: all 0.3 ease;
transition: all 0.3 ease;
cursor: pointer;

}

<body>
<form align = center method = post>
<div class = "form">
<?php
session_start ();
if ($_SESSION["LoggedIn"] != "true") {
    echo '<h1>Restricted Access</h1>';
    die();
}
$now = time(); // Checking the time now when home page starts.

    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo "<h1>Session expired. <a     href='http://omarsdajani.alwaysdata.net/Admin/Login.html'>Login</a></h1>";
        die ();
    }

    if ($_GET) {
        if(isset($_GET['Find Player'])){
            echo ("Weee");
            DisplayTable();
        } else {
            echo ("Weee 2");
        }
    }

    function DisplayTable () {
        session_start ();
        include ("Connect.php");
        $conn = Connect ();
        $displayName = $_POST ['displayNamePost'];
        $stmt = mysqli_stmt_init ($conn);
        if (mysqli_stmt_prepare ($stmt, "SELECT * FROM Users WHERE   Display_Name = ?")) {
            mysqli_stmt_bind_param ($stmt, "s", $displayName);
            mysqli_stmt_execute ($stmt);
            $result = mysqli_stmt_get_result ($stmt);
            if (mysqli_num_rows ($result) == 1) {
                echo "<table><tr><th>ID</th><th>Name</th><th>Display   Name</th></tr>";
                while ($row = mysqli_fetch_assoc ($result)) {
                    echo "<tr><td>".$row["ID"]."</td><td>".$row["First_Name"]." ".$row["Last_Name"]."</td><td>".
                    $row["Display_Name"]."</tr>";
                }
                echo "</table>";
            }
        }
    }
?>
    <h1 align = "center" id = header>User Info Panel</h1><br/>
    <input type = "text" placeholder = "User display name" name = "displayNamePost"/> <br/><br/>
    <input type = "Submit" id = button value = "Find Player" name = "Find Player"/>
</div>
</form>
</body>
</html>
Answers: