2013-02-15 88 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檢查你的數據庫值。