2015-11-25 76 views
0

我有一個簡單的腳本,它使用AJAX查找數據庫表中的更改,並且如果檢測到更改,則返回該表中的信息。結合jquery,AJAX和PHP的問題

這裏是我的源:http://blog.codebusters.pl/en/ajax-auto-refresh-volume-ii/#comment-570

我也有寫在JS/JQ這個漂亮的通知橫幅:http://www.jqueryscript.net/other/Simple-jQuery-Sticky-Notification-Plugin-Notify-Me.html

我已經從獨立工作,如,更新程序會自動更新事業部,並通知將在按鈕按下時激活。

這裏是更新程序的接收DIV的代碼:

<div id="message-list" data-counter="<?php echo (int)$db->check_changes();?>"> 
    <?php echo $db->get_news();?> 
</div> 

據我看到的,當數據計數器的變化,然後get_news()被調用。

這裏是get_news() - 這是一個類下定義的PHP函數:

function get_news(){ 
    if($result = $this->dbase->query('SELECT * FROM cmc_log WHERE id<>1 ORDER BY time DESC LIMIT 1')){ 
     $return = ''; 
     while($r = $result->fetch_object()){ 
      $return .= '<p> '.$r->time.' | '.htmlspecialchars($r->message).'</p>'; 
     } 
     return $return; 
    } 
} 

對於通知系統,我有:

<div class="container"> 

    <div class="btn-group"> 
     <a class="btn error"><i class="fa fa-warning"></i> Error</a>  
    </div> 
</div> 

<!-- SCRIPTS --> 
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script type="text/javascript" src="assets/js/notifyme.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $('.error').on('click', function(){ 
     $(this).notifyMe(
      'top', 
      'error', 
      'Lorem Ipsum Text', 
      'Lorem ipsum dolos lorem uisnsnd h jsakdh ajkdbh', 
      600 
     ); 
    }); 
}); 
</script> 

我想通知旗幟被稱爲當新聞可用,即在我猜get_news()。但是我也需要將get_news()的輸出結合到實際的橫幅中......我想我已經弄糊塗了什麼可以去哪裏了!

任何幫助表示讚賞!謝謝,

更新!因此,自動刷新腳本的作者發佈並提醒了我關於AJAX,這當然是觸發的理想場所。

<script> 
    /* AJAX request to checker */ 
    function check(){ 

     $.ajax({ 
      type: 'POST', 
      url: 'checker.php', 
      dataType: 'json', 
      data: { 
       counter:$('#message-list').data('counter') 
      } 
     }).done(function(response) { 
      /* update counter */ 

      $('#message-list').data('counter',response.current); 

      /* check if with response we got a new update */ 

      if(response.update==true){ 
       $('#message-list').html(response.newsf); 
       $('#message-list2').html(response.news); 
       //fire notification 

       $(this).notifyMe(
'top', 
'error', 
'Update available:', 
response.news.val, 
400); 

      } 
     }); 
    } 
    //Every 10 sec check if there is new update 
    setInterval(check,10000); 
</script> 

我還沒有制定出的最後一件事是如何得到的$('#message-list2').html(response.news);值作爲一個字符串到notifyMe()調用?

回答: 明白了這個工作: 有可能比填充一個隱藏的div一個更合適的方法,但至少現在工作!

<script> 
    /* AJAX request to checker */ 
    function check(){ 

     $.ajax({ 
      type: 'POST', 
      url: 'checker.php', 
      dataType: 'json', 
      data: { 
       counter:$('#message-list').data('counter') 
      } 
     }).done(function(response) { 
      /* update counter */ 

      $('#message-list').data('counter',response.current); 

      /* check if with response we got a new update */ 

      if(response.update==true){ 
       $('#message-list').html(response.newsf); 
       $('#message-list2').html(response.news); 
       //fire notification 

       $(this).notifyMe('top','error','Update available:',document.getElementById('message-list2').innerHTML,400); 

      } 
     }); 
    } 
    //Every 20 sec check if there is new update 
    setInterval(check,10000); 
</script> 

Previous alerts: 

<div style="padding-left:10px" id="message-list" data-counter="<?php echo (int)$db->check_changes();?>"> 
    <?php echo $db->get_news_full();?> 
</div> 

<div style="display: none;" id="message-list2" data-counter="<?php echo (int)$db->check_changes();?>"> 
    <?php echo $db->get_news();?> 
</div> 

尼克

+0

那你得到什麼錯誤? –

+0

我並不完全看到一個問題 - 你在哪裏遇到麻煩? –

+0

我建議調用後面的'檢查'內的支票。完成「,因爲如果連接失敗,你將繼續建立請求 – birdspider

回答

1

需要觸發點擊的時候有一個新的條目,阿賈克斯完成後和response.update是真實的。類似的東西:

function check(){ 
     $.ajax({ 
      type: 'POST', 
      url: 'checker.php', 
      dataType: 'json', 
      data: { 
       counter:$('#message-list').data('counter') 
      } 
     }).done(function(response) { 
      /* update counter */ 
      $('#message-list').data('counter',response.current); 
      /* check if with response we got a new update */ 
      if(response.update==true){ 
       $('#message-list').html(response.news); 

       //we have new entries so we want to open the notify thing, so we trigger click, I'm assuming the click is for $('.error') 
       $('.error').trigger('click'); 
      } 
     }); 
    } 
+0

當然!我在通知中收到舊消息,但不是最新消息!?例如在http://patientpathfinder.co.uk/superstorefinder_responsive/ticker.php感謝您的快速回復!很高興您的現場演示備份;-) –

0
<script> 
    /* AJAX request to checker */ 
    function check(){ 

     $.ajax({ 
      type: 'POST', 
      url: 'checker.php', 
      dataType: 'json', 
      data: { 
       counter:$('#message-list').data('counter') 
      } 
     }).done(function(response) { 
      /* update counter */ 

      $('#message-list').data('counter',response.current); 

      /* check if with response we got a new update */ 

      if(response.update==true){ 
       $('#message-list').html(response.newsf); 
       $('#message-list2').html(response.news); 
       //fire notification 

       $(this).notifyMe('top','error','Update available:',document.getElementById('message-list2').innerHTML,400); 

      } 
     }); 
    } 
    //Every 20 sec check if there is new update 
    setInterval(check,10000); 
</script> 

Previous alerts: 

<div style="padding-left:10px" id="message-list" data-counter="<?php echo (int)$db->check_changes();?>"> 
    <?php echo $db->get_news_full();?> 
</div> 

<div style="display: none;" id="message-list2" data-counter="<?php echo (int)$db->check_changes();?>"> 
    <?php echo $db->get_news();?> 
</div>