AJAX检索股利每60秒股利、AJAX

由网友(吃素的狮子)分享简介:我希望做的是检索一个PHP文件的内容,并将其插入一个div每60秒,基本上刷新动态分区。我想出了以下code,到目前为止,但它似乎并不奏效。在code就是这样的,没有什么多余的,除​​了MYSQL登录。What I would like to do is retrieve the contents of a PHP...

我希望做的是检索一个PHP文件的内容,并将其插入一个div每60秒,基本上刷新动态分区。我想出了以下code,到目前为止,但它似乎并不奏效。在code就是这样的,没有什么多余的,除​​了MYSQL登录。

What I would like to do is retrieve the contents of a PHP file and insert it into a div every 60 seconds, basically refreshing the dynamic div. I've come up with the following code so far, however it doesn't seem to be working. The code is just like this, nothing extra, apart from the MYSQL login.

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 code是一样的,封闭在一个div与id属性ajax_table。

The original PHP code is just the same, enclosed in a div with an id attribute of 'ajax_table'.

在JavaScript是:

The JavaScript is:

// 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页面在页面上。

and that is on the page that is requesting the other php page.

推荐答案

如何使用像jQuery框架,以简化您的javascript:

How about using a framework like jQuery to simplify your javascript:

$(function() {

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

});
阅读全文

相关推荐

最新文章