title: How can I continuously update a PHP variable without refreshing a page?
body: <p>I'm making a website that shows data from a MySQL database in Morris graphs. Basically I have a database that gets new measurements every minute, and I'm trying to show these changes live without having to reload the whole page. I have shortened my code for this question, but here's basically what I have:</p>
  
  <p>PHP code:</p>
  
  <pre><code>&lt;?php
     while($measurement = mysqli_fetch_array($result)){
         $data += $measurement['data'];
     }
  ?&gt;
  </code></pre>
  
  <p>And the script:</p>
  
  <pre><code>function data() {
          var ret = [];
          ret.push({
            y: 'Today',
            a: &lt;?php echo $data; ?&gt;
          });
        return ret;
      }
  
  var graph = Morris.Bar({
          element: 'graph',
          data: data(),
          xkey: 'y',
          ykeys: ['a'],
          labels: ['random label']
      });
  
  function update() {
        graph.setData(data());
      }
  
  setInterval(update, 60000);
  </code></pre>
  
  <p>The graph is then shown in a div with the id "graph". So the problem is that the update function doesn't update the graph with new data as $data doesn't get updated. I've heard that I can somehow create a PHP function and continuously run that using Ajax and have it update my $data variable, but I have no idea how to do that.</p>
  
  <p>In order for the graph to update I have to reload the whole page, and this works fine using a meta tag that resfreshes the page every 60 seconds, but it seems like a bad solution.</p>
  
  <p>I have also tried to put the code in a separate PHP file and run that using this code:</p>
  
  <pre><code>var auto_updater = setInterval(
      (function () {
          $("#graph").load("data.php");
      }), 60000);
  </code></pre>
  
  <p>This also works fine, but the problem is that it redraws the whole graph and that causes the scroll bar on my site to go crazy. What I want is to update the data variable in Morris.Bar, not the whole thing. Any help would be appreciated.</p>