Javascript - load php data from database automatically
<p>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.</p>

<p>index.php - > shows data from load.php automatically, but now shows only if I refresh load.php</p>

<p>load.php - > gets data from database</p>

<p>index.php:</p>

<pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$(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
});
&lt;/script&gt;
&lt;div id="responsecontainer"&gt;&lt;/div&gt;
</code></pre>

<p>load.php</p>

<pre><code>            &lt;?php 


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn-&gt;connect_error) {
    die("Connection failed: " . $conn-&gt;connect_error);
} 

$sql = "SELECT * FROM users WHERE type_account='1'";

$result = $conn-&gt;query($sql);



    while($row = $result-&gt;fetch_assoc()) {
       $username =  $row["name"];
$userid = $row["id"];     
}


$sql2 = "SELECT * FROM posts WHERE user='$userid' AND status='1'";

$result2 = $conn-&gt;query($sql2);



    while($row2 = $result2-&gt;fetch_assoc()) {
 $postdetails = $row2["post_details"];

?&gt;
Popular Microblogs:&lt;br&gt;
&lt;br&gt;
&lt;span style="color:#fff;"&gt;
&lt;?php
echo $username; 
echo "&lt;br&gt;";
echo $postdetails;
echo "&lt;br&gt;";
}


echo "&lt;br&gt;";  



$conn-&gt;close();

echo "&lt;/span&gt;";


?&gt; 
</code></pre>