Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP jQuery: Passing a PHP variable FROM (not to) $.load()
    primarykey
    data
    text
    <p>I've seen questions asking about sending variables TO a document being loaded in $.load, but not retrieving variables from a $.load.</p> <p>I've pasted the pertinent pieces of code below; essentially what I'm trying to do is run a PHP function every so often, and initially when the page first loads.</p> <p>When the page first loads, it runs the getData function - and everything works as intended. But later down the page, when I try to load pullData.php, srcAverage doesn't update with the new value. The JS alert shows the srcAverage value.</p> <p>Example: The first time the page is run, srcAverage is X. Every 5 seconds, we want to load pullData.php and update srcAverage on index.php with the new value (change X). </p> <p>I feel like it's something really small I'm doing incorrectly - ideas?</p> <p>conn.php</p> <pre><code>&lt;?php define("HOST", "stuff"); define("USER", "stuff"); define("PASSWORD", "stuff"); define("DATABASE", "stuff"); $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE); // Connection info above all works as intended ?&gt; </code></pre> <p>index.php</p> <pre><code>&lt;?php include 'inc/conn.php'; include 'inc/function.php'; $src = "none"; 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;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 &lt;?php echo $srcAverage; ?&gt;'); }, 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"; getData($src, $mysqli); ?&gt; </code></pre> <p>The getData function (see code below) grabs 4 values from separate tables, averages them together (I have them all separated in different statements and variables for troubleshooting purposes), then sets the variable srcAverage to the average value. I've tested that the MySQLi statements are working fine, and srcAverage <em>is</em> assigned the correct value by the function. Echoing or JS alerting show the value as intended (on this page). But the variable does NOT get passed to index.php when loaded via load().</p> <p>function.php</p> <pre><code>&lt;?php function getData($src, $mysqli) { // Check SRC for specific source // If no specific source, get average of all sources // If YES specific source, get that value global $srcAverage; if ($src == 'alt') { if ($stmt = $mysqli-&gt;prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { $stmt-&gt;execute(); // Execute the prepared query. $stmt-&gt;store_result(); $stmt-&gt;bind_result($altVal); // get variables from result. $stmt-&gt;fetch(); if($stmt-&gt;num_rows == 1) { // The entry exists, good to go // echo $altVal; } } else { // Either no results pulled or more than one. echo "Error pulling alternate data!"; return false; } } else { // Value 1 if ($stmt = $mysqli-&gt;prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { $stmt-&gt;execute(); // Execute the prepared query. $stmt-&gt;store_result(); $stmt-&gt;bind_result($firstVal); // get variables from result. $stmt-&gt;fetch(); if($stmt-&gt;num_rows == 1) { // echo $firstVal; // This works as intended } } else { // Either no results pulled or more than one. echo "Error pulling first value data!"; return false; } // Value 2 if ($stmt = $mysqli-&gt;prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { $stmt-&gt;execute(); // Execute the prepared query. $stmt-&gt;store_result(); $stmt-&gt;bind_result($secondVal); // get variables from result. $stmt-&gt;fetch(); if($stmt-&gt;num_rows == 1) { // The entry exists, good to go // echo $secondVal; } } else { // Either no results pulled or more than one. echo "Error pulling second value data!"; return false; } // Value 3 if ($stmt = $mysqli-&gt;prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { $stmt-&gt;execute(); // Execute the prepared query. $stmt-&gt;store_result(); $stmt-&gt;bind_result($thirdVal); // get variables from result. $stmt-&gt;fetch(); if($stmt-&gt;num_rows == 1) { // The entry exists, good to go // echo $thirdVal; } } else { // Either no results pulled or more than one. echo "Error pulling third value data!"; return false; } // Value 4 if ($stmt = $mysqli-&gt;prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { $stmt-&gt;execute(); // Execute the prepared query. $stmt-&gt;store_result(); $stmt-&gt;bind_result($fourthVal); // get variables from result. $stmt-&gt;fetch(); if($stmt-&gt;num_rows == 1) { // The entry exists, good to go // echo $fourthVal; } } else { // Either no results pulled or more than one. echo "Error pulling fourth value data!"; return false; } // So everything up to this point is working fine. Statements grab data as intended, and assign variables. // We have data - move forward $srcCount = 4; $srcTotal = $firstVal + $secondVal + $thirdVal + $fourthVal; $srcAverage = $srcTotal / $srcCount; $srcAverage = number_format((float)$srcAverage, 2, '.', ''); // echo "Total: $srcTotal .... Average: $srcAverage"; // If we were to echo above, it would display correctly. Problem is passing the variable to index return $srcAverage; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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