2013-02-15 83 views
1

我有一个简单的问题,我敢肯定。我只是不知道我应该在谷歌搜索什么。它可能会更容易让我解释一下:AJAX/PHP自动显示更新值

比如我有一个MySQL字段值“是”

我如何使用AJAX/PHP保存查询字段当值变为“没有'?

有人能解释,简单来说取悦

+0

也许'setInterval()'? :) – 2013-02-15 09:23:23

回答

0

有两个功能,将有所帮助。

setTimeout (expression, timeout); 
setInterval (expression, interval);

expression是一个函数和timeout和间隔是在milliseconds整数。 setTimeout运行一次计时器并运行一次表达式,而setInterval将在每次间隔过去时运行表达式。

所以你的情况,将工作是这样的:

setInterval(function() { 
    //call $.ajax here 
    $.ajax({ 

     url   : URL, 
     data  : passData, 
     dataType : 'json', //or html or xml 
     beforeSend : function() 
     { 
      //this will execute before request is send 
     }, 
     success  : function(response) 
     { 
      //check for response if(response) { } else { } 
     } 

    }); 
}, 5000); //5 seconds

现在后端PHP文件。

<?php 

$passedVar = $_REQUEST['passedData']; //get data that were passed in ajax call 
//database connection 
//query to check for status 
if(query return true) 
{ 
    echo json_encode(true); 
    exit; 
} 
else 
{ 
    echo json_encode(false); 
    exit; 
}
0

首先创建一个JavaScript函数将执行Ajax调用然后把该功能上的setInterval()

function ajaxcall(){ 

// you ajax call; 
} 


setInterval(ajaxcall, 10000);// change time by replacing 10000(time is in millisecond) 

这里AjaxCall的将在每10秒钟内叫。你可以在ajaxcall函数中做任何事情我的意思是通过ajax检查你的数据库值。