2011-12-16 59 views
1

第一次來我這裏。更新反映在數據庫中,但不是SELECT查詢

我使用PHP,MySQL和JavaScript和運行jQuery和AJAX功能。

我有一個運行功能的錨鏈接。

該函數將會清空DIV標記,然後填充/顯示器(SELECT查詢)行的表中DIV與爲空where子句。每行都有一個選擇框,其中NULL數據列將放在哪裏。選擇框中的數據來自不同的表格。

當選擇框改變它立即運行AJAX在選擇框中輸入新的數據來更新錶行。

當你點擊鏈接再次運行功能(空,選擇查詢和顯示基於空列),這是剛剛更新到數據庫的行顯示再次基礎上,爲空子句。在這一點上檢查數據庫表顯示它實際上不是空的,並在第一次出現時進行了正確的UPDATE。

頁在此過程中從來沒有刷新,從來沒有要。如果我刷新它顯示沒有BUG的正確數據。

所有你的想法,不勝感激。

亞光

function closeItemsBtn() { // called by a click function of a standard link/button 
    $('#CloseItems').empty(); // remove all html from the DIV 
    var newAppend = ''; 
    <? 
    //[... connect to db ...] 
    // select info from 2 different tables so it can be used in different locations if necessary 
    // where the row has not been reviewed and therefore is null 
    $query = "select * from nearmiss where reviewedId is null order by submitDate desc"; 
    $result = $db->query($query) or die($db->error); 
    $query2 = "select * from hazardid where reviewedId is null order by submitDate desc"; 
    $result2 = $db->query($query2) or die($db->error); 
    $num_rows = $result->num_rows; 
    $num_rows2 = $result2->num_rows; 
    // create html for the DIV tag. Creates a table in a DIV that collapses by clicking aCloseList. 
    // each row is in tbody so it can be striped by a all purpose striping function 
    // this part is the table header and opening div tags and link 
    $newAppend = "<p id=\"closeList\"><a href=\"#\" id=\"aCloseList\">Show/Hide</a> {$num_rows} Near Misses requiring attention.</p><div id=\"closenearmiss\" style=\"display:none;\"><table class=\"closenearmisstable\"><tbody><tr><td>Date Submitted</td><td>Submitted By</td><td>Location</td><td>Reviewed By</td></tr><tr><td rowspan=\"2\">Type</td><td colspan=\"3\">Description/Observation</td></tr><tr><td colspan=\"3\">Action/Reinforcement</td></tr></tbody>"; 
    // update various foreign key information from other tables 
    for ($i=0;$i<$num_rows;$i++) { 
$row = $result->fetch_assoc(); 
$query3 = "select location from locations where locationId='{$row['locationId']}'"; 
$result3 = $db->query($query3); 
$location = $result3->fetch_assoc(); 
$query3 = "select name from employees where employeeId='{$row['employeeId']}'"; 
$result3 = $db->query($query3); 
$name = $result3->fetch_assoc(); 
     // here is the table itself with the select tag in the null column name=reviewed 
$newAppend .= "<tbody><tr><td>{$row['submitDate']}</td><td>{$name['name']}</td><td>{$location['location']}</td><td><form name=\"nearmissreview\" action=\"\" method=\"\"><input type=\"hidden\" name=\"docId\" value=\"{$row['nearmissId']}\"><input type=\"hidden\" name=\"type\" value=\"nearmiss\"><select name=\"reviewed\"><option>Choose name to sign off</option></select></form></td></tr><tr><td rowspan=\"2\">Near Miss</td><td colspan=\"3\">{$row['description']}</td></tr><tr><td colspan=\"3\">{$row['action']}</td></tr></tbody>"; 
    } 
$newAppend .= "</table></div>"; 

// this is the beginning of the second table same structure as first with collapsing but 
     // different data 
$newAppend .= "<p id=\"closeList\"><a href=\"#\" id=\"aCloseList\">Show/Hide</a> {$num_rows2} Hazard IDs requiring attention.</p><div id=\"closehazardid\" style=\"display:none;\"><table class=\"closehazardidtable\"><tbody><tr><td>Date Submitted</td><td>Submitted By</td><td>Location</td><td>Reviewed By</td></tr><tr><td rowspan=\"2\">Type</td><td colspan=\"3\">Description/Observation</td></tr><tr><td colspan=\"3\">Action/Reinforcement</td></tr></tbody>"; 
    for ($i=0;$i<$num_rows2;$i++) { 
$row = $result2->fetch_assoc(); 
$query3 = "select location from locations where locationId='{$row['locationId']}'"; 
$result3 = $db->query($query3); 
$location = $result3->fetch_assoc(); 
$query3 = "select name from employees where employeeId='{$row['employeeId']}'"; 
$result3 = $db->query($query3); 
$name = $result3->fetch_assoc(); 
$newAppend .= "<tbody><tr><td>{$row['submitDate']}</td><td>{$name['name']}</td><td>{$location['location']}</td><td><form name=\"hazardidreview\" action=\"\" method=\"\"><input type=\"hidden\" name=\"docId\" value=\"{$row['hazardidId']}\"><input type=\"hidden\" name=\"type\" value=\"hazardid\"><select name=\"reviewed\"><option>Choose name to sign off</option></select></form></td></tr><tr><td rowspan=\"2\">Hazard ID</td><td colspan=\"3\">{$row['description']}</td></tr><tr><td colspan=\"3\">{$row['action']}</td></tr></tbody>"; 
    } 
$newAppend .= "</table></div>"; 
echo "newAppend='{$newAppend}';"; 
$result->free(); 
$result2->free(); 
$result3->free(); 
$db->close(); 
?> 
// put HTML of $newAppend php in the DIV 
$('#CloseItems').append(newAppend); 
// fill the select box with a variable set somewhere else in the code not displayed here 
$('#CloseItems select[name=reviewed]').append(newAppendE); 
stripePointsTable('.closenearmisstable tbody'); 
stripePointsTable('.closehazardidtable tbody'); 
// close list click options 
$('#closeList > a').each(function() { 
$(this).click(function() { 
    if ($(this).parent().next().css('display')=='none') { 
     $(this).parent().next().slideDown('slow'); 
    } else { 
     $(this).parent().next().slideUp('fast'); 
    } 


}); 
}); 
// select tag change function that calls ajax and displays a message in a DIV called header 
$('#closenearmiss select').change(function() { 
    function processDataClose(data, success) { 
     if (success) { 
      $('#header').prepend(data+"<br />"); 
      closeItemsBtn(); 
     } else { 
      $('#header').prepend(data+"<br />"); 
     } 
    } 
    var formData = $(this).parent().serialize(); 
    $.post('processreviewsign.php',formData,processDataClose); 
}); 
+2

設置cache: false這是幾乎可以肯定在某些方面存在緩存問題。是否有可能爲我們提供一些簡約的代碼片段來更好地理解頁面的運行方式? – Nicholas 2011-12-16 15:51:33

+0

你可以發佈一些你的代碼嗎? – 2011-12-16 15:52:11

回答

0

的jQuery緩存您的Ajax調用。這很可能是您刷新頁面時看到正確結果的原因,而不是在通過ajax進行的後續調用中。

嘗試在ajax call