I need to fetch data from mysql database and show it in php file. Then on homepage I need to load it automatically without reloading a page. I have achieved the result, but the data is loading automatically only when I refresh the php file.
index.php - > shows data from load.php automatically, but now shows only if I refresh load.php
load.php - > gets data from database
index.php:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
function load() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "views/index/info.php",
dataType: "html", //expect html to be returned
success: function (response) {
$("#responsecontainer").html(response);
setTimeout(load, 1000)
}
});
}
load(); //if you don't want the click
$("#display").click(load); //if you want to start the display on click
});
</script>
<div id="responsecontainer"></div>
load.php
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users WHERE type_account='1'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$username = $row["name"];
$userid = $row["id"];
}
$sql2 = "SELECT * FROM posts WHERE user='$userid' AND status='1'";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc()) {
$postdetails = $row2["post_details"];
?>
Popular Microblogs:<br>
<br>
<span style="color:#fff;">
<?php
echo $username;
echo "<br>";
echo $postdetails;
echo "<br>";
}
echo "<br>";
$conn->close();
echo "</span>";
?>
#display?url: "views/index/info.php",but your file isload.php. Last time I checkedinfo!=load.