SavageBooBoo
SavageBooBoo

Reputation: 35

no output when requesting date/time from php file

Ive created a php file with the current date and time. i also have a html/javascript file which tries to request the output of the php and add it to the html. Im currently getting no response text back and im not sure why.

Bare in mind im a noob at ajax and this is a simple exercise to get learn ajax

This is the html/javascript:

<body>
      <p>Current server time:</p>
      <div id="poll"></div>

      <script>
    function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("poll").innerHTML =
          this.responseText;
        }
      };
      xhttp.open("GET", "polling.php", true);
      xhttp.send();
    }
    </script>
      </body>

This is my php:

    <?php

echo date("D M j H:i:s e Y");

?>

Upvotes: 1

Views: 38

Answers (1)

Gopalakrishnan
Gopalakrishnan

Reputation: 957

You just declared the javascript function, but not made the call that function while page load

<body>
<p>Current server time:</p>
<div id="poll"></div>

  <script>
   function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("poll").innerHTML =
            this.responseText;
        }
      };
      xhttp.open("GET", "polling.php", true);
      xhttp.send();
   }
   loadDoc(); // ***here you missed****
  </script>
</body>

Upvotes: 1

Related Questions