2013-10-30 75 views
-1

我曾經見過有關向$ .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; 
} 

} 
?> 
+0

@Evan從問題傳遞變量從PHP中的看起來到Javascript,而不是Javascript到PHP。 – MWJump

+0

@Evan:不是這個問題的重複。正如MWJump上面所說 - 我想從文件傳遞,而不是傳遞。 – Lent

回答

0

你的問題是你呼應$ srcAverage通過PHP。瀏覽器不從服務器獲取php或知道如何執行它,它只能看到php腳本的結果。所以,如果你查看源代碼,你會看到你所有的警報是:

alert('New value is '); 

的PHP已經運行,$ srcAverage當它呼應了未定義由於範圍,因此把其轉換爲一個空字符串。

由$ .load加載的內容是pullData.php的結果,已經被php解析並通過服務器返回。 pullData.php沒有輸出。您需要回顯getData的結果,以便javascript可以看到它。

您需要在JavaScript中做的是:根據您的示例標記

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

也似乎缺少#targetDiv。你可能想要在#targetDiv中包裝原始的getData。

所以,您的固定版本是這樣的:

的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); 
?> 
+0

謝謝 - 您的解決方案相當有幫助和信息。它也有效:)標記這個解決方案是最好的,因爲它最適合問題的上下文。 在function.php中,我在返回之前回顯了srcAverage,然後按照建議更改了JS中的警報 - 警報顯示了預期的值。 謝謝! – Lent

0

把它作爲在服務器上有了新的說法

$("#element").load("file.php", { 'myVar' : 'someValue'}); 

然後:

$_POST['myVar']; 

將包含值。

編輯

也許我誤的理解,如果你想從負載功能訪問數據,你需要使用回調。

$('#element').load('file.php', function(data){ 
    //response from the server 
}); 
+0

這與OP試圖做的事情相反 –

1

你誤會了php頁面的生命週期。 <?php echo $srcAverage; ?>僅在最初加載/呈現頁面時評估一次。

如果你想獲得新的價值,你或許應該切換到使用$.ajax()代替​​,並pullData.php呼應的結果json這樣你就可以在JavaScript中響應工作。

+0

嗯,這是有道理的。我只需要使用json。謝謝:) – Lent