Trigger php file from javascript
<p>I have a file 'data.php' that handles incoming posts, updating and reading from a database. The posts comes every second. </p>

<p>And I have a file 'demo.php' with js script, the idea is that I want to present the data from the database in a graph. ..so it has to update every sec or so. How can i do that, do i need to trigger the php file someway to get new data to the js variable?</p>

<p>data.php:</p>

<pre><code>&lt;?php
     include "k_connect.php";
     if(isset($_POST['value']))
     {

        $amp_f = number_format($_POST['value'], 2);
        $amp = explode('.', $amp_f);
        $d_tid = gmdate('Y-m-d H:i:s');

        $amp =str_replace(",","",$amp);
        $tid = explode(' ',$d_tid);
        $tid = str_replace(":","",$tid);

        mysql_query("UPDATE impdata SET amp=$amp[0]
        WHERE ID=1");
      }

      $tid_back=array();
      $amp_back = array();

      $x=0;
      $SQL = "SELECT * FROM impdata";
      $result = mysql_query($SQL);
      while ( $db_field = mysql_fetch_assoc($result) ) {
        $amp_back["amp"] = $db_field['amp'];
        $tid_back["tid"] = $db_field['tid'];    
        $amp_s =  $amp_s . json_encode($amp_back) . " ";
        $tid_s = $tid_s . json_encode($tid_back) . " ";
        $x++;
      }
</code></pre>

<p>?></p>

<p>demo.php:</p>

<pre><code>  function graf() {
       var amp = &lt;?php echo json_encode($amp_s); ?&gt;;
       var power = amp.split(":");
       alert(power[1].match(/\d+/g));
       var req = new XMLHttpRequest();
       setTimeout(graf, 500);
  }
  graf();
</code></pre>

<p>I get the right value in demo.php('power' holds it), but just one time, it  it doesn't  update, it is just the same value, dho if i hit f5 i get a new value.</p>

<p>My idea was :  'data.php' runs every time there is a post (every sec) and then the value of '$amp_s' is updated, that way my js function updates as well because it reads the value of '$amp_s'(setTimeout(graf, 500)).  </p>

<p>Is there anything wrong with the way I put it up?</p>

<p>I have this in demo.php of course:</p>

<pre><code>include "k_connect.php";
include('data.php');
</code></pre>
