数据库更改后使用Javascript重新加载页面 [英] Reload Page with Javascript after Database changes

查看:91
本文介绍了数据库更改后使用Javascript重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中显示一个表格

I'm displaying a Table from a Database like so

<tr>
  <td>{{ $user->id }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td></td>
</tr>

如果数据库中有更改,我必须在浏览器中点击F5才能显示更改。现在我想使用Javascript刷新页面,每次数据库中发生变化时。我不想在一段时间内刷新页面。

我有一个管理员可以更改某人的状态,您可以自己更改自己的状态。当有人更改自己的状态时,管理员后端应该刷新。

If something changes in the Database I have to hit F5 in the Browser to Display the changes. Now I want to refresh the Page with Javascript, each time something changes in the Database. I don't want to refresh the page on a time intervall.
I have an Admin who can change someones Status and you can change your status yourself. When someone changes his own status the Admin Backend should refresh.

你们有什么想法吗?

推荐答案

最简单的方法(已经提到)将是每x秒重新加载页面。

The easiest way (which has already been mentioned) would be to reload the page every x seconds.

如果用户正在浏览/编辑页面上的任何内容,那么在所有进度丢失之前,他只有x秒才能完成此操作。

This means however, that if the user is browsing / editing anything on the page, he only has x seconds to finish this before all the progress is lost.

您可以使用AJAX来检查数据库用于更改但是:

You could use AJAX to check the database for changes however:


  • 创建一个DB-Check.php脚本,输出最新的数据库条目

  • 在你的主文件中,包含一个AJAX脚本,它每30秒提取
    DB-Check.php

  • 将DB-check.php的输出放在一个新的JS变量

  • 如果新变量与旧变量不同,请刷新
    整个页面(或使用AJAX加载数据库的其余部分)

  • Make a DB-Check.php script that outputs the latest database entry
  • In your main file, include an AJAX script which pulls the DB-Check.php every 30 seconds
  • Put the output from the DB-check.php in a new JS variable
  • If the new variable is different from the old variable, refresh the whole page (Or use AJAX to load the rest of the database)

这篇关于数据库更改后使用Javascript重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆