1

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>
Daniel
  • 189
  • 1
  • 7
  • With no GET or POST request.. Why? Also, `xmlhttp.open("GET",results.php,true);` should have quote marks around results.php, since its a string (not a variable name). – enhzflep May 18 '14 at 17:10

1 Answers1

0

Pushing data out to the client as opposed to responding to a request for data requires an open connection to the client be maintained for the duration of the availability of the push. There are various different ways to achieve this type of connection, but two of the more popular approaches are long polling and web sockets. Here's some reading:

Community
  • 1
  • 1
oliakaoil
  • 1,615
  • 2
  • 15
  • 36