AJAX检索股利每60秒 [英] AJAX Div Retrieval every 60 seconds

查看:133
本文介绍了AJAX检索股利每60秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望做的是检索一个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);

});

这篇关于AJAX检索股利每60秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆