2012-08-31 27 views
0

我從內容爲動態的xml文件(file.xml)加載數據,但問題是表格中的內容相同,但我在文件中看到內容更改。就好像加載完成一次。我的表格中的內容沒有更改

請幫忙。

謝謝

的代碼和平:(對不起,如果你發現我的代碼有問題)

window.setInterval(function() { 
     <?php 
     $xml = simplexml_load_file("file.xml"); 
     foreach($xml as $node) 
     { 
      $name = ""; 
      $value = -1; 

      foreach($node->attributes() as $a => $b) { 
       if($a == "name") 
       { 
        $name = (string)$b; 
       } 
       else if($a == "value") 
       { 
       $value = (string)$b; 
       } 
      } 

      $vars[$name] = $value; 
     } 

     $json = json_encode($vars); 
     ?> 

     //// code to show the result in my table 
    }, 1000); 

回答

1

PHP代表PRE超文本處理,這意味着PHP是首先產生。

此外,您還需要了解PHP在服務器端處理,而JavaScript在客戶端處理。

所以當你的PHP函數運行時,它會按預期加載文件,但只會加載一次。您需要將PHP函數放在它自己的.php文件中,然後每秒使用一次AJAX請求來獲取更新的表。 jQuery中

例如,假設你的PHP代碼是loadtable.php

window.setInterval(function() { 
    $.ajax({ 
     method : "GET", 
     url : "loadtable.php", 
     success : function(data){ 
      //set the body of the page to the result of the PHP file. 
      $("body").html(data); 
     } 
    }); 
}, 1000);