php-每60秒检索AJAX Div

我想做的是检索PHP文件的内容,然后每60秒将其插入div中,基本上刷新了动态div.到目前为止,我已经提出了以下代码,但是它似乎没有用.代码就是这样,除了MYSQL登录外,没有什么额外的.

PHP抢:

<?php                   
$time = date("m/d/Y h:i:s a", time());
                mysql_query("UPDATE djs SET requesttime='{$time}' WHERE username='{$djs['username']}'")
                or die(mysql_error()); 

                $request_db = mysql_query("SELECT * FROM requests
                WHERE haveplayed='0'") or die(mysql_error());  
                echo "<table style=\"border:1px solid;width:99%;margin-left:auto;margin-right:auto;\" border=\"1\">";
                echo "<tr><th>Title</th><th>Artist</th><th>Dedicated To...</th></tr>";
                while($request = mysql_fetch_array( $request_db )) {
                    echo "<tr><td style=\"width:33%;padding:1px;\">";
                    echo $request['SongName'];  
                    echo "</td><td style=\"width:33%;\">";
                    echo $request['Artist'];    
                    echo "</td><td style=\"width:33%;\">";
                    echo $request['DedicatedTo'];
                    echo "</td></tr>";  
                }
                echo "</table>";
            ?>

原始的PHP代码是相同的,包含在div中,其id属性为’ajax_table’.

JavaScript是:

// JavaScript Document
var xmlHttp_moniter

function moniter()
{
    xmlHttp_moniter = GetXmlHttpObject_parcel()
if(xmlHttp_moniter == null)
{
    alert("browser does not support HTTP Request")
    return
}
var url="ajax_table.php?random=" + Math.random()
xmlHttp_moniter.onreadystatechange = stateChanged
xmlHttp_moniter.open("GET",url,true)
xmlHttp_moniter.send(null)

}

function stateChanged()
{
if(xmlHttp_moniter.readyState==4 || xmlHttp_moniter.readyState == "complete")
{
    document.getElementById("ajax_table").innerHTML = xmlHttp_moniter.responseText
    setTimeout('ajax_table()',60000);
}
}

function GetXmlHttpObject_parcel()
{
var xmlHttp_moniter=null;
try
{
    xmlHttp_moniter=new XMLHttpRequest();
}
catch (e)
     {
         //Internet Explorer
         try
          {
              xmlHttp_moniter=new ActiveXObject("Msxml2.XMLHTTP");
          }
         catch (e)
          {
          xmlHttp_moniter=new ActiveXObject("Microsoft.XMLHTTP");
          }
     }
return xmlHttp_moniter;
}

那是在请求另一个php页面的页面上.

最佳答案
如何使用类似jQuery的框架来简化您的javascript:

$(function() {

  setInterval(function() {
    $.get('ajax_table.php', function(data) {
      $('#ajax_table').html(data);
    });
  }, 60 * 1000);

});

转载注明原文:php-每60秒检索AJAX Div - 代码日志