admin管理员组

文章数量:1335617

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>

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.

Do you guys have any ideas on that?

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>

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.

Do you guys have any ideas on that?

Share Improve this question edited Sep 8, 2014 at 9:08 jrenk asked Sep 8, 2014 at 8:26 jrenkjrenk 1,4164 gold badges27 silver badges47 bronze badges 4
  • 1 If you're wanting real-time updating then you'll want to look into websockets. – Ben Fortune Commented Sep 8, 2014 at 8:32
  • What about sending request for example each 30 seconds to a server, where a script will check if there were updates, returns answer to a client and it reloads or not the page. Also you can look at long polling github./panique/php-long-polling . – heroin Commented Sep 8, 2014 at 8:33
  • possible duplicate of How to automatically reload a web page at a certain time? – Anil Bhattarai100 Commented Sep 8, 2014 at 8:41
  • I'm quiet new to Javascript so Websockets are not so optimal for me as a beginner, but thanks for your suggestion. I think I will try the Long-Polling or Short-Polling Method. Thanks for your answer! – jrenk Commented Sep 8, 2014 at 8:48
Add a ment  | 

6 Answers 6

Reset to default 1

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

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.

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

  • 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)

Fetch the data from database using ajax after some time interval.

Loop through the fields you got from DB and pare with current fields in page.

If you find any changed then update it on the front end with the data you've already fetched.

The easiest solution is to refreshing the page automatically in second. So, please add this inside the head:

<meta http-equiv="refresh" content="30" />

to refresh it every 30 seconds. Others ways are by javascript:

setTimeout('window.location.reload();', 30000);

or

setTimeout('history.go(0);', 30000);

or

setTimeout("location.reload(true);", timeoutPeriod);
location.reload();

this is the javascript function to reload the current page. You can use this function inside the js function when do you want this.

this Question is a bit very large, so if you want the correct answer according to your scenarios please describe your detail scenarios.

you may be looking for like this How to automatically reload a web page at a certain time?

I would do this using very primitive API. jQuery (or JS) makes request to API url which returns boolean, and using this boolean, true would trigger refresh page. (or even better- change content without with jQuery without refreshing) that would be my approach.

 // AJAX Request
            $.ajax({
                url: 'remove.php',
                type: 'POST',
                data: { id:id },
                success: function(response){
    
                    if(response == 1){
                        // Remove row from HTML Table
                        $(el).closest('div').css('background','tomato');
                        $(el).closest('div').fadeOut(800,function(){
                            $(this).remove();
                        });
                     location.reload();   
                    }else{
                        alert('Invalid ID.');

本文标签: phpReload Page with Javascript after Database changesStack Overflow