简体   繁体   English

jQuery在数据库更新后不更新div内容

[英]jQuery not updating div content after database update

I am loading a php file in a div using jQuery. 我正在使用jQuery在div中加载一个php文件。 The php file displays the content of a MySQL database. php文件显示MySQL数据库的内容。 It works fine but here is the problem: if I make a change to the database, I add a row for example, the div does not reflect the change. 它工作正常,但这是问题所在:如果我对数据库进行了更改,例如,我添加了一行,则div无法反映更改。 Whatever I do I just see the data as they were the first time I accessed the page. 无论我做什么,我都会看到数据,因为它们是我第一次访问页面时的数据。

The code I am using is pretty simple: 我使用的代码非常简单:

// This goes into head
<script src="jquery-1.10.2.min.js">
</script>
<script type="text/javascript">
function recp(id) {
$('#myStyle').load('displaydata.php?id=' + id);
}
</script>
// Set id to '1' onload
<body onload="recp('1')">
// Then I have a few links that will change the value of id onclick
<a href="#" onClick="recp('1')">Materials</a> | <a href="#" onClick="recp('2')">Manufacturing</a>
// Finally my div
<div id='myStyle'>
</div>

I do not put here the displaydata.php code. 我不在这里放置displaydata.php代码。 Anyway it is just a simple php file that SELECT and ECHO data. 无论如何,这只是一个简单的php文件,其中包含SELECT和ECHO数据。

Does anyone know what I am doing wrong? 有人知道我在做什么错吗? Why the div does not reflect the changes I make to the MySQL database? 为什么div不能反映我对MySQL数据库所做的更改? Thanks. 谢谢。

I would suggest you to think of a problem in terms of caching. 我建议您考虑缓存方面的问题。 If you are okay with moving away from load method, you could take a look at following code and make it into a right use. 如果您可以放弃load方法,可以看一下以下代码并将其正确使用。

var recp = function(id) {
  $.ajax({
      url: '/displaydata.php?id=' + id,
      cache: false,
      dataType: 'html',
      success: function(data) {
          $('#myStyle').html(data);
      }
  });
};

Hope it helps. 希望能帮助到你。

Is your php page cached? 您的php页面是否已缓存? If you're making changes to the database behind the scenes, you need to clear your cache. 如果要在后台更改数据库,则需要清除缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM