-1

$.load に読み込まれているドキュメントに変数を送信することについての質問を見たことがありますが、$.load から変数を取得することはできません。

関連するコードを以下に貼り付けました。基本的に私がやろうとしているのは、ページが最初にロードされたときに、PHP関数を頻繁に実行することです。

ページが最初に読み込まれると、getData 関数が実行され、すべてが意図したとおりに機能します。しかし、ページの後半で、pullData.php を読み込もうとすると、srcAverage が新しい値で更新されません。JS アラートは srcAverage 値を示します。

例: ページが初めて実行されるとき、srcAverage は X です。5 秒ごとに、pullData.php をロードし、index.php の srcAverage を新しい値で更新します (X を変更します)。

私が間違ってやっている本当に小さなことのように感じます-アイデア?

conn.php

<?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
?>

index.php

<?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
?>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function() {
    // each interval, get first and second values
    $("#targetDiv").load("pullData.php");
    alert('New value is <?php echo $srcAverage; ?>');
    }, 5000); // end setInterval
});
</script>

pullData.php

<?php
include 'incl/conn.php';
include 'incl/function.php';
$src = "none";
getData($src, $mysqli);
?>

getData 関数 (以下のコードを参照) は、個別のテーブルから 4 つの値を取得し、それらを平均して (トラブルシューティングのために、それらをすべて異なるステートメントと変数に分けています)、変数 srcAverage を平均値に設定します。MySQLi ステートメントが正常に機能していることをテストしました。関数によって srcAverageに正しい値が割り当てられます。エコーまたは JS アラートは、(このページで) 意図したとおりに値を表示します。しかし、load() 経由でロードされた場合、変数は index.php に渡されません。

関数.php

<?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->prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { 
      $stmt->execute(); // Execute the prepared query.
      $stmt->store_result();
      $stmt->bind_result($altVal); // get variables from result.
      $stmt->fetch();

      if($stmt->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->prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { 
      $stmt->execute(); // Execute the prepared query.
      $stmt->store_result();
      $stmt->bind_result($firstVal); // get variables from result.
      $stmt->fetch();

      if($stmt->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->prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { 
      $stmt->execute(); // Execute the prepared query.
      $stmt->store_result();
      $stmt->bind_result($secondVal); // get variables from result.
      $stmt->fetch();

      if($stmt->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->prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { 
      $stmt->execute(); // Execute the prepared query.
      $stmt->store_result();
      $stmt->bind_result($thirdVal); // get variables from result.
      $stmt->fetch();

      if($stmt->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->prepare("SELECT value FROM table ORDER BY id DESC LIMIT 1;")) { 
      $stmt->execute(); // Execute the prepared query.
      $stmt->store_result();
      $stmt->bind_result($fourthVal); // get variables from result.
      $stmt->fetch();

      if($stmt->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;
}

}
?>
4

3 に答える 3

1

PHP ページのライフサイクルを誤解しています。<?php echo $srcAverage; ?>ページが最初にロード/レンダリングされるときに一度だけ評価されます。

新しい値を取得したい場合は、おそらく$.ajax()の代わりにを使用するように切り替え、JavaScript で応答を操作できるように結果.load()pullData.phpエコーする必要があります。json

于 2013-10-30T17:26:28.967 に答える
0

あなたの問題は、php を介して$srcAverageをエコーアウトしていることです。ブラウザは、サーバーから php を取得したり、それを実行する方法を認識したりせず、php スクリプトの結果のみを確認します。したがって、ソースを表示すると、すべてのアラートが次のようになっていることがわかります。

alert('New value is ');

PHPは既に実行されており、エコーされたときの$srcAverageはスコープのために定義されていなかったため、空の文字列に変換されるnullを配置しました。

$.load によって読み込まれるのは、pullData.php の結果であり、既に php によって解析され、サーバーを通じて返されます。pullData.php には出力がありません。javascript が認識できるように、getData の結果をエコーする必要があります。

JavaScriptで行う必要があるのは次のとおりです。

alert('New value is ' + $('#targetDiv').html());

マークアップの例に基づいて、#targetDiv も欠落しているようです。おそらく、元の getData を #targetDiv でラップする必要があります。

したがって、修正版は次のようになります。

index.php

<div id='targetDiv'>
<?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
?>
</div>

<script type="text/javascript">
$(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
});
</script>

pullData.php

<?php
include 'incl/conn.php';
include 'incl/function.php';
$src = "none";
echo getData($src, $mysqli);
?>
于 2013-10-30T17:33:24.623 に答える