-1
在我的顯示頁面我有一個腳本,更新另一個PHP頁面的div。通用函數更新DIV
如何將下面的腳本重寫爲: 是一個函數,我可以在我的顯示頁面(在php中)調用updateadiv(divid,content)
,其中content是一個php變量。 (腳本將不會調用php頁面,而是輸入變量)。因爲PHP和JavaScript運行在不同的環境
<!DOCTYPE html>
<html>
<body>
function updatediv(divId, content)
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
(function($)
{
$(document).ready(function()
{
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#content').hide();
$('#loading').show();
},
complete: function() {
$('#loading').hide();
$('#content').show();
},
success: function() {
$('#loading').hide();
$('#content').show();
}
});
var $container = $("#content");
$container.load("http://192.168.1.90/json_output.php");
var refreshId = setInterval(function()
{
$container.load('http://192.168.1.90/json_output.php');
}, 9000);
});
})(jQuery);
</script>
<?php
function createarray() {
global $reindexed_devices_a ;
$feed_url = "http://192.168.1.90/JSON?request=getstatus&ref=all&location1=all&location2=all";
//$feed_url = "demoxml.xml";
$json = file_get_contents($feed_url);
$devices_a = json_decode($json, true);
//echo $devices_a[Devices][2][name] ;
//echo "<BR> ReIndexed:";
$reindexed_devices_a = array(Devices => array());
foreach ($devices_a[Devices] as $value) {
$reindexed_devices_a[Devices][$value[ref]] = $value;
}
//echo $reindexed_devices_a[Devices][59][name] ;
//need to do some conditional formatting before updating to DIV's
if ($reindexed_devices_a[Devices][59][name] == 'Coffee maker') {
$reindexed_devices_a[Devices][59][name] = "overwritten";
}
echo time();
//echo $reindexed_devices_a[Devices][59][name] ;
}
createarray();
$name59=$reindexed_devices_a[Devices][59][name];
//check if $name59 has changed - if yes update div
updatediv('59');
echo "This is a test <div id = 'name59'>.....</div>";
?>
</body>
</html>
需要更詳細地解釋並沒有顯示在這裏沒有PHP代碼。你的問題目前很難理解 – charlietfl 2014-09-28 12:01:02
@charlietfl - 謝謝,我已經更新了問題的代碼部分。基本上,我試圖做的是使用PHP來設計所有的邏輯和條件格式,並創建到函數,加載JSON持有一些設備和他們的狀態到一個PHP數組,在PHP中做一些格式化(如值對於設備x <20,然後使字體藍色),和一個更新相應的div – user1546642 2014-09-28 15:42:46
仍然不是100%明確具體問題是什麼。如果返回json'load()'不會有幫助,因爲它是用於html的,所以在收到它之後需要將json解析爲html。此外,問題仍然指'php變量'沒有任何解釋 – charlietfl 2014-09-28 16:15:47