Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problem is you are echoing out <strong>$srcAverage</strong> through php. The browser doesn't get php from the server or know how to execute it, it only sees the result of the php script. So, if you view source you will see that all your alert is is:</p> <pre><code>alert('New value is '); </code></pre> <p>The php already ran, <strong>$srcAverage</strong> when it was echoed was undefined due to scope so it put a <strong>null</strong> which converts to an empty string.</p> <p>What gets loaded by $.load is the result of pullData.php, already parsed by php and returned through the server. pullData.php has no output. You need to echo the result of getData so that the javascript can see it.</p> <p>What you need to be doing in javascript is:</p> <pre><code>alert('New value is ' + $('#targetDiv').html()); </code></pre> <p>Based on your example markup you also seem to be missing #targetDiv. You probably want to wrap your original getData in #targetDiv.</p> <p>So, your fixed version looks like this:</p> <p>index.php</p> <pre><code>&lt;div id='targetDiv'&gt; &lt;?php include 'inc/conn.php'; include 'inc/function.php'; $src = "none"; echo getData($src, $mysqli); // This initial run of getData works as intended // Skip to further down // The JS alert below does NOT reflect the new srcAverage ?&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { setInterval(function() { // each interval, get first and second values $("#targetDiv").load("pullData.php"); alert('New value is ' + $('#targetDiv').html(); }, 5000); // end setInterval }); &lt;/script&gt; </code></pre> <p>pullData.php</p> <pre><code>&lt;?php include 'incl/conn.php'; include 'incl/function.php'; $src = "none"; echo getData($src, $mysqli); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload