Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a PHP page into a HTML div

Tags:

html

php

mysql

So I am currently trying to create a portion of my website that fetches a mySQL table. I wrote the php to fetch the data, create a table, and print out the table in a separate php file named display.php. Now i am trying to load it into a div on my html page but for some reason it is not working. I am using XAMPP and php is working as my login page works. Here is the code:

HTML: (I have tried both include and require)

<html>
<body>
    <div id="display" class="myCustom1">
       <?php require('display.php'); ?>
    </div>

    <div id="display" class="myCustom1">
        <?php include("display.php"); ?>
    </div>
</body>
</html>

PHP: (if i run just the php page it creates the table)

<?php
$servername = "localhost";
$username = "test";
$password = "test";
$database = "data1";

//create connection & access DB
$connect = mysqli_connect("$servername","$username","$password","$database"); 

//if error, display error
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($connect,"SELECT * FROM data1enter");

echo 
"
    <table border='1'>
    <tr>
    <th>Song</th>
    <th>Author</th>
    </tr>
";

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

mysqli_close($connect);
?>
like image 800
liamlows Avatar asked Jun 12 '26 13:06

liamlows


1 Answers

You are using <?php ?> tags in the html file, Change the extension of the file containing the below code to .php from .html and you will be able to use include function.

<html>
<body>
    <div id="display" class="myCustom1">
       <?php require('display.php'); ?>
    </div>

    <div id="display" class="myCustom1">
        <?php include("display.php"); ?>
    </div>
</body>
</html> 

Also make sure the include path is correct. Hope it helps !

like image 165
Pardeep Poria Avatar answered Jun 15 '26 02:06

Pardeep Poria



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!