使用ajax在3秒后自动从mysql数据库中获取数据

时间:2015-07-04 23:50:07

标签: javascript php html

SELECT FROM * FROM table_name;

我希望每隔3秒后自动将数据提取到没有点击事件的div或带有ajax的onchange事件

请帮助家伙

3 个答案:

答案 0 :(得分:0)

为了自动更新div中的东西,你应该使用Ajax和Js计时器。

示例(使用jQuery):

var inProcess = false;//Just to make sure that the last ajax call is not in process
setTimeout( function () {
    if (inProcess) {
        return false;//Another request is active, decline timer call ...
    }
    inProcess = true;//make it burn ;)
    jQuery.ajax({
        url: 'info.php', //Define your script url here ...
        data: '', //Pass some data if you need to
        method: 'POST', //Makes sense only if you passing data
        success: function(answer) {
            jQuery('#mydiv').html(answer);//update your div with new content, yey ....
            inProcess = false;//Queue is free, guys ;)
        },
        error: function() {
            //unknown error occorupted
            inProcess = false;//Queue is free, guys ;)
        }
    });
}, 3000 );

答案 1 :(得分:0)

这是我的info.php

<?php

include('connect.php');
$sql = "select * from admin";
$result  = mysql_query($sql);
while($row = mysql_fetch_array($result)){
echo $row[0]."<br>";
}



?>

答案 2 :(得分:0)

function myfunction(){
var setint = setInterval(function(){
var x;
 if(window.XMLHttpRequest){
        x = new XMLHttpRequest();
}else{
            x = new ActiveXObject("Microsoft.XMLHTTP");
   }
x.onreadystatechange=function(){
if(x.readyState==4 && x.status==200){
    document.getElementById('mydiv').innerHTML=x.responseText;
}
}
x.open("GET","info.php",true);
x.send();

},500);                 }