我曾經見過有關向$ .load中加載的文檔發送變量的問題,但沒有從$ .load中檢索變量。
我粘貼了下面相關的代碼片段;基本上我想要做的是每隔一段時間運行一次PHP函數,最初在頁面首次加載時運行。
當頁面第一次加載時,它運行getData函數 - 並且一切按預期工作。但後來在頁面下方,當我嘗試加載pullData.php時,srcAverage不會使用新值更新。 JS警報顯示srcAverage值。
示例:首次運行頁面時,srcAverage爲X.每隔5秒鐘,我們要加載pullData.php並使用新值(更改X)更新index.php上的srcAverage。
我覺得它真的很小我做錯了 - 想法?
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倍的值,平均值他們在一起(我把它們全部用不同的語句和變量分開以便進行故障排除),然後se將變量srcAverage設置爲平均值。我已經測試過MySQLi語句工作正常,並且srcAverage 是由函數指定了正確的值。回聲或JS警報顯示預期值(在此頁面上)。但是當通過load()加載時,變量不會傳遞給index.php。
function.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;
}
}
?>
@Evan從問題傳遞變量從PHP中的看起來到Javascript,而不是Javascript到PHP。 – MWJump
@Evan:不是這個問題的重複。正如MWJump上面所說 - 我想從文件傳遞,而不是傳遞。 – Lent