2012-02-23 5 views

答えて

2

具体的なチュートリアルはありませんが、AJAXを学び、JavaScriptのsetIntervalメソッドを使用して2分ごとにAJAX呼び出しを行うだけです。


EDIT

MEH、私はこの例を記述するのに十分な退屈。これはテストされていませんが、エラーはないと思います。

<html> 
<head> 
    <script type="text/JavaScript"> 
     window.onload = function() 
     { 
      // call your AJAX function every 2 minutes (120000 milliseconds) 
      setInterval("getRecords()", 120000); 
     }; 

     function getRecords() 
     { 
      // create the AJAX variable 
      var xmlhttp; 
      if (window.XMLHttpRequest) 
       xmlhttp = new XMLHttpRequest(); 
      else 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 

      // set up the response function 
      xmlhttp.onreadystatechange = function() 
      { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
       { 
        /* 
         Your code goes here. 'xmlhttp.responseText' has the output from getRecords.php 
        */ 
        document.getElementById("txaRecords").innerHTML = xmlhttp.responseText; 
       } 
      } 

      // make the AJAX call 
      xmlhttp.open("GET", "getRecords.php", true); 
      xmlhttp.send(); 
     } 
    </script> 
</head> 
<body> 
    <textarea id="txaRecords"></textArea> 
</body> 
</html> 
+0

はどうもありがとうございました。できます! – jaypabs

+0

@ user1034801:うまくいけば、これをあなたの受け入れられた回答としてマークしてください。ありがとう! – Travesty3

+0

私は投票できません。投票には15の評判が必要です。 – jaypabs

1

これは私がこの正確な目的のために書いたコードです。必要に応じて適応する。

AJAXコード:

function timer() 
{ 
var t=setTimeout("check()",2000); 
// At an appropriate interval 
} 


function check(){ 

     var xmlhttp; 

     if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
      } 
     else 
      {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
     xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
      if (xmlhttp.responseText!=""){ 
       var output = xmlhttp.responseText; 
       // Do what you need to do with this variable 
       } 
      } 
      } 
     xmlhttp.open("GET","backend.php",true); 
      // Set file name as appropriate. 
     xmlhttp.send(); 
     timer(); 
     } 

PHPコード:

<?php 

// This assumes you have already done mysql_connect() somewhere. 

// Replace as appropriate 
$query = "SELECT * FROM table_name"; 

// Perform the query 
$result = mysql_query($query); 

// Get the results in an array 
while($row = mysql_fetch_array($result)) { 
    // Echo the message in an appropriate format.     
    echo "<br />" . $row['column_name']; 
} 
?> 

は、ページをロードするようJS関数の1つを開始することを忘れないでください:

<body onload="timer()"> 
関連する問題