2012-03-22 26 views
0

我有這段代碼,我該如何讓它成爲ajax jquery?我有一個正在從數據庫拉的文章的頁面,我希望一旦用戶點擊按鈕刷新Ajax調用將刷新文章。ajax jquery刷新數據庫中的數據

  <!-- refresh the page with jquery !--> 
      <script type="text/javascript"> 
       $(document).ready(function() {  
       $('#Button_refresh').click(function() {  
       $.ajax({ 
}); }); 
      </script> 

回答

0

客戶端javascript無法從數據庫中進行查詢。那麼,至少它不是如何在安全的應用程序中完成的。你可以從後端(php/asp/python/nodejs ..)詢問應該這樣做的情況。

如果你的意思是,以喚起一些DIV通過URL從後端的文章,那麼就

$.get(URL, function(response){ 
    $('#wrapper').html(response); 
}); 
0

您可以通過一個文件夾中創建一個PHP文件(如果你使用PHP)和使用做到這一點將php文件作爲控件(用於檢索數據)。首先你必須做ajax「Post」呼叫。下面是一些代碼:

$.ajax({ 
     type: "POST", 
     url: "linktoyourphpfile.php", 
      data: { refresh: articles }, 
      success: function(data) 
        { 
       $("#div-id-where-articles-are-generated").html(data); 

        } 
      }); 

      }); 

現在正在通話後,您可以編輯PHP文件

if (isset($_POST['refresh']) { 

// you do the query here. It will be an array. So you need a to define a string and accumulate whatever you need to be written. here's a sample. 
$str=""; //first define the string. 

$resultset //is the array of the results returned by the database. then loop through it. 

foreach($resultset as $res) 
    { 
     $str=$str."<div>$res['somethin']</div>"; //you got the point. 

    } 
//then at the end just echo the str. 

echo $str; 
}