2015-04-19 97 views
1

我用自動刷新功能做了一個彈出消息,所以每隔幾分鐘彈出窗口就會出現以顯示記錄。它的工作。Popup消息無法顯示來自php mysql的新更新記錄

以下是JavaScript代碼,自動刷新:顯示彈出消息

$(document).ready(function() { 
    setInterval(function() { 
     $('#rtnotice').load('plugins/notice/n_notice_invoice.php').fadeIn("slow"); 
    }, 5000) 
}); 

n_notice_invoice.php

<script> 
    $(document).ready(function(){ 
     $("#hide").click(function(){ 
      $("#noticearea").hide(); 
     }); 
    }); 
    </script> 

<?php 
    try{ 
    require_once "../../config/c_config.php"; 
    $db = dbConn::getConnection(); 
    $timestamp = $_REQUEST['term']; 
    $sqlck = $db->prepare("SELECT COUNT(id_notice_alert) as ttlinv FROM str_notice_alert WHERE date_alert > '$timestamp'"); 
    $sqlck->execute(); 
    $resck = $sqlck->fetch(PDO::FETCH_ASSOC); 
    if($resck['ttlinv'] == '0') 
    {}else{ 
    ?> 
    <div id="noticearea"> 
    <div id="modal"> 
      <div class="modal-content"> 
       <div class="header"> 
        <div id="circle" align="center"><h1><?php echo $resck['ttlinv'];?></h1></div><div class="titlenotice"><h1>NOTICE ALERT<?php echo $timestamp; ?></h1></div> 
        <div class="break"></div> 
       </div> 
       <div class="copy"> 
        <p> 
        <table width="100%" class="gridtable"> 
        <tr><th>No</th><th>Name</th><th>Status</th><th>Location</th><th>Date</th></tr> 
    <?php 
    $sqll = $db->prepare("SELECT * FROM str_notice_alert"); 
    $sqll->execute(); 
    while($resl = $sqll->fetch(PDO::FETCH_ASSOC)){ 
    ?> 
    <tr><td align="center"><?php echo $resl['id_notice_alert']; ?></td><td><?php echo $resl['alert_name']; ?></td><td align="center"><?php echo $resl['alert_status']; ?></td><td align="center"><?php echo $resl['alert_location']; ?></td><td><?php echo $resl['date_alert']; ?></td></tr> 
    <?php } ?> 
    </table> 
        </p> 
       </div> 
       <div class="cf footer"> 
        <button id="hide" class="btn">Close</button> 
       </div> 
      </div> 
      <div class="overlay"></div> 
     </div></div> 
    <?php 

    $sqltrunc = $db->prepare("TRUNCATE TABLE str_notice_alert"); 
    $sqltrunc->execute(); 

    }$db = null;} 
    catch (PDOException $e) { 
    echo "Connection Error " . $e->getMessage(); 
    } 
    ?> 

的代碼後,會顯示文件n_notice_invoice.php現有記錄並通過可用查詢刪除記錄。在任何外觀。但問題是,爲什麼記錄沒有更新/更改。除非我直接刷新文件n_notice_invoice.php,然後自動刷新顯示最新的數據。

+0

您可以顯示n_notice_invoice.php頁? –

+0

@lelio faieta:我上面顯示,謝謝 –

+0

編輯我的答案,告訴你什麼是問題 –

回答

1
$timestamp = $_REQUEST['term']; 

應該在每次打電話給頁面時更新。您應該使用傳遞$ timestamp作爲參數的Ajax加載頁面,而不是僅加載該頁面。

爲了得到你需要的東西,我建議你使用長輪詢? PHP長時間輪詢,或者使用node.js更好。對於PHP例如,在「服務器」頁:

$timeStart = time(); 
// Create connection 
$con = mysqli_connect('localhost','root','','polldb'); 

// Check connection 
if (mysqli_connect_errno($con)) 
    die ('Failed to connect to MySQL: ' . mysqli_connect_error()); 

// select where item is new 
if(isset($_POST['timestamp'])){ 
    $timestamp = $_POST['timestamp']; 
}else{ 
    // get current database time 
    $row = mysqli_fetch_assoc(mysqli_query($con,'SELECT now() as now')); 
    $timestamp = $row['now']; 
} 
$sql = "SELECT * FROM `notification` WHERE timestamp > '$timestamp'"; 

$newData = false; 
$notifications = array(); 

// loop while there is no new data and is running for less than 20 seconds 
while(!$newData && (time()-$timeStart)<20){ 

    // check for new data 
    $result = mysqli_query($con,$sql); 
    while($row = mysqli_fetch_assoc($result)){ 
     $notifications[] = $row; 
     $newData = true; 
    } 
    // let the server rest for a while 
    usleep (500000); 
} 

// get current database time 
$row = mysqli_fetch_assoc(mysqli_query($con,'SELECT now() as now')); 
$timestamp = $row['now']; 
mysqli_close($con); 

// output 
$data = array('notifications'=>$notifications,'timestamp'=>$timestamp); 
echo json_encode($data); 
exit; 

和客戶端:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
<script> 
$(function(){ 
    pullNotification(); 
}); 

function pullNotification(timestamp){ 
    var data = {}; 
    if(typeof timestamp!='undefined') 
     data.timestamp = timestamp; 

    $.post('longpoll.php',data,function(msg){ 

     var newData = ''; 
     for(i in msg.notifications){ 
      newData+=msg.notifications[i].message+'\n'; 
     } 
     if(newData!='') 
      alert(newData); 
     pullNotification(msg.timestamp); 
    },'json'); 
} 
</script> 

這將檢查數據庫的更新,並會彈出起來。每隔20秒它將更新請求。顯然你必須適應你的需求。

+0

謝謝,我會嘗試 –

0

我believie您的問題來自緩存的請求(在瀏覽器上的AJAX本身) 請嘗試禁用AJAX緩存:

$(document).ready(function() { 
    setInterval(function() { 
     $.ajax({ 
     url: "plugins/notice/n_notice_invoice.php", 
     cache: false 
     }) 
     .done(function(data) { 
      $('#rtnotice').html(data).fadeIn("slow"); 
     }); 
    }, 5000) 
}); 
+0

其實他並沒有做一個Ajax調用,而只是模擬它。 –