I don't know how to fetch data with no request, I've been googling but it seems request is mandatory. In this example I just want to load data asynchronously. If database is updated by admin there's no need for the client to refresh the page.
Thanks in advance.
results.php
<?php
$con=mysqli_connect("host","xxx","xxx","xxx");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Pages");
while($row = mysqli_fetch_array($result))
{
echo "<a href=" . $row['URL'] . ">" . $row['URL'] . "</a>";
echo "<br>";
}
mysqli_close($con);
?>
index.html
<html>
<script>
function showLinks() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("links_area").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",results.php,true);
xmlhttp.send();
}
</script>
//<body onload="showLinks()"> FIRST TRY, IT DIDN'T WORK
//<button onclick="showLinks()">Click</button> SECOND TRY, IT DIDN'T WORK NEITHER
<div id="links_area"><img src="loading.gif"/></div>
</body>
</html>