2014-09-28 57 views
-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> 
+0

需要更详细地解释并没有显示在这里没有PHP代码。你的问题目前很难理解 – charlietfl 2014-09-28 12:01:02

+0

@charlietfl - 谢谢,我已经更新了问题的代码部分。基本上,我试图做的是使用PHP来设计所有的逻辑和条件格式,并创建到函数,加载JSON持有一些设备和他们的状态到一个PHP数组,在PHP中做一些格式化(如值对于设备x <20,然后使字体蓝色),和一个更新相应的div – user1546642 2014-09-28 15:42:46

+0

仍然不是100%明确具体问题是什么。如果返回json'load()'不会有帮助,因为它是用于html的,所以在收到它之后需要将json解析为html。此外,问题仍然指'php变量'没有任何解释 – charlietfl 2014-09-28 16:15:47

回答

0
 
function updateadiv(DivID , Url){ 


    $('#loading').hide(); 
     $('#' + DivID).html(''); 

     $.ajax({ 
      url:url, 
      success:function(rData){ 
      $('#' + DivID).html(rData); 
     } 

     }); 
}