php - UPDATE 反射(reflect)在 DB 但不是 SELECT 查询

标签 php javascript jquery mysql ajax

我第一次来这里。

我正在使用 PHP、MySQL、JavaScript 并运行 JQUERY 和 AJAX 函数。

我有一个运行函数的 anchor 链接。

该函数清空 DIV 标记,然后使用 is null where 子句填充/显示(SELECT 查询)DIV 中的行表。每行都有一个选择框,空数据列将放在其中。选择框中的数据来自不同的表。

当选择框发生变化时,它会立即运行 ajax 以使用选择框中的新数据更新表格行。

当你点击链接再次运行函数时(空,选择query and display based on null column)刚刚更新到DB的行再次基于is null子句显示出来。此时检查数据库表表明它实际上不为空,并且在第一次检查时已正确更新。

在此过程中,页面永远不会刷新,也不必刷新。如果我确实刷新,它会显示正确的数据而没有 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);
});

最佳答案

jQuery 缓存您的 ajax 调用。这可能就是为什么您在刷新页面时看到正确结果,但在通过 ajax 进行的后续调用中却看不到的原因。

尝试在您的 ajax call 中设置 cache: false

关于php - UPDATE 反射(reflect)在 DB 但不是 SELECT 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8536605/

相关文章:

javascript - 自动加载更多帖子(检查元素在屏幕上是否可见)

jquery - CSS3动画淡出一个图像并淡入另一个图像

jQuery 不淡入淡出文本?

php - 标题数量可变的表的 MySQL 表结构

php - 如何在不列出目录或使用curl传输文件的情况下发送FTP命令?

php - Codeigniter 中的路由 - 找不到 404 页面

javascript - 尝试使用 gruntjs 设置 Phaser 和 Typescript

php - 显示和隐藏按钮 (href=) 如果文件存在于服务器中

javascript - jQuery 中的单击事件调用两次或多次

javascript - jQuery 中如何判断一个元素是否存在?