2011-10-22 22 views
0

私は10秒ごとにPHPスクリプトをリフレッシュする必要があります。これはアップデートシステム用です。新しいアップデートを探して、javascript/phpに値を返してからdivのユーザに表示しますか?10秒ごとにPHPスクリプトをリフレッシュする方法はありますか?

これが最良の方法ですか、そうでない場合は?

私はすべてのコメントに開かれています。

ありがとうございます。その後、

答えて

4

は、JS関数内でPHPスクリプトにあなたのAJAX呼び出しを設定し、関数の最後の行は、あなたがどこかの関数を最初に呼び出す必要があります、ああsetTimeout(functionname, 10000);

こと、および必要がありますそれはそう...実行していきます。

<html><head> 
<script> 
function update(){ 
//your ajax here 
setTimeout(update,10000); 
} 
update(); 
</script></head> 
<body><div id="theDivToUpdate"></div></body></html> 

ここではいくつかのサンプルコードを使用すると、JSONやXMLを渡すことができる方法をお見せし、HTMLページがそれを処理する必要があります。そして、ここではサンプルのHTMLページです

<?php 
$format=$_REQUEST['format']; 
if($format=="json"){ 
    header('Content-type: application/json'); 
    header('Cache-Control: no-cache, must-revalidate'); 
    echo "{\"status\":\"ok\",\"results\":[{\"first\":\"MyFirst\",\"last\":\"MyLast\",\"company\":\"Big Company\"},{\"first\":\"YourFirst\",\"last\":\"YourLast\",\"company\":\"Another Company\"},{\"first\":\"John\",\"last\":\"Doe\",\"company\":\"What Company?\"},{\"first\":\"Jane\",\"last\":\"Doe\",\"company\":\"Stackoverflow\"}]}"; 
} 
else if($format=="xml"){ 
    header("Content-type: text/xml"); 
    header('Cache-Control: no-cache, must-revalidate'); 
    echo "<ROOT><STATUS>ok</STATUS><RESULTS><RESULT><FIRST>MyFirst</FIRST><LAST>MyLast</LAST><COMPANY>Big Company</COMPANY></RESULT><RESULT><FIRST>YourFirst</FIRST><LAST>YourLast</LAST><COMPANY>Another Company</COMPANY></RESULT><RESULT><FIRST>John</FIRST><LAST>Doe</LAST><COMPANY>What Company?</COMPANY></RESULT><RESULT><FIRST>Jane</FIRST><LAST>Doe</LAST><COMPANY>Stackoverflow</COMPANY></RESULT></RESULTS></ROOT>"; 
    } 

else{ 
    echo "You need to supply the \'format\' parameter (json or xml)"; 
} 
?> 

と::

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script> 

function getJSON(){ 
     var xmlhttp; 
     xmlhttp=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")); 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       var res=xmlhttp.responseText; //THE JSON STRING IS TEXT 
       res=JSON.parse(res); //THIS CONVERTS THE JSON INTO OBJECTS THAT CAN BE ACCESSED WITH DOT NOTATION 
       if(res.status=="ok"){ 
        newtable="<table><tr><th>First</th><th>Last</th><th>Company</th></tr>"; 
        for(i=0;i<res.results.length;i++){ 
         newtable+="<tr><td>"+res.results[i].first+"</td><td>"+res.results[i].last+"</td><td>"+res.results[i].company+"</td></tr>"; 
        } 
        newtable+="</table>"; 
        document.getElementById('resultDiv').innerHTML=newtable; 
       } 
      } 
     } 
     xmlhttp.open("GET","thePHPFile.php?format=json",true); 
     xmlhttp.send(); 
} 

function getXML(){ 
     var xmlhttp; 
     xmlhttp=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")); 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       res=xmlhttp.responseXML; 
       firstNameFields=res.documentElement.getElementsByTagName("FIRST"); //THESE LINES GRAB ALL OF THE NODES WITH THE INDICATED NAME AND HOLD THEM IN ARRAYS 
       lastNameFields=res.documentElement.getElementsByTagName("LAST"); 
       companyFields=res.documentElement.getElementsByTagName("COMPANY"); 
       newtable="<table><tr><th>First</th><th>Last</th><th>Company</th></tr>"; 
       for(i=0;i<firstNameFields.length;i++){ 
        newtable+="<tr><td>"+firstNameFields[i].firstChild.nodeValue+"</td><td>"+lastNameFields[i].firstChild.nodeValue+"</td><td>"+companyFields[i].firstChild.nodeValue+"</td></tr>"; 
       } 
       newtable+="</table>"; 
       document.getElementById('resultDiv').innerHTML=newtable; 
      } 
     } 
     xmlhttp.open("GET","thePHPFile.php?format=xml",true); 
     xmlhttp.send(); 
} 

</script> 
</head> 

<body> 
<input type="button" onclick="getJSON()" value="Retrieve and Parse JSON" /> 
<input type="button" onclick="getXML()" value="Retrieve and Parse XML" /> 
<div id="resultDiv"></div> 
</body> 
</html> 
+0

PHPファイルの配列から配列が返されますか? ajax.responseTextから? –

+0

しかし、XMLやJSONのようにフォーマットして操作しやすいようにしてください –

+0

ああ、responseTextを実行する場合はJSON ... responseXMLを使用する場合はXML –

0

それがに表示されていた場合はここではPHP(ここではデータは単にハードコードされていますが、あなたのケースでは、あなたが動的に移入したい)ですWebブラウザでは、setInterval()関数でajax呼び出しを行い、サーバを更新用にポーリングします。だから、リフレッシュをJavaScriptではなく、PHPに入れてください。

関連する問題