开发者

alert another page

I have an index.php. It has a log in function. Let's say I have 2 users A and B.

Page1(index.php of user A) -with function(checks whether there are changes in the database, if yes: alert('message'))

Page2(index.php of user B) -with button which when clicked should update the database

I can already update the database, the function from page1 already works, it can detect changes, but only when you reload it. So I want to reload Page1 whenever page2 updates the database. I thought of just using setTimeout(something, timeInterval) in Page1 but it doesn't seem appropriate since the components of Page1 are flickering everytime it reloads. I want Page1 to reload if and only if there are changes in my database. I also thought of just putting the Page1's function inside an infinite while loop but it doesn't seem appropriate either, since the page just loads..

Another thing is, I was informed about JSON and REST. So I could just access the database from the script file. But I'm not that knowledgeable in it. Neither am I in jQuery.

I've tried searching about refreshing a page from another but the answers I found were either vague or difficult for me. And my pages have the same name, they only differ with the user logged in. But If I can find a way to do 开发者_如何学Gothis, then I'll just do something with my pages' names.

Is there any other way I can do this? Thank you.


So you basically need for the server to notify page1 when an update is available. What you are looking is called "comet". Check out this articles:

comet wikipedia

comet ajaxian

Comet programing can be done using ajax or iframes


When the page you want to update first loads, put the time in a variable by doing

something = new Date().getTime();

Send an XMLHttpRequest to a php that checks if the databases last update time is newer than the time sent with the ajax call. If it is, return something that tells the javascript to refresh the page. If not, have the javascript make another call in a few seconds with setTimeout .

It would be better if you learned the ropes of javascript before using jQuery, so I'll update this answer in a second with some script.

edit 1: check out https://developer.mozilla.org/en/AJAX/Getting_Started

Using jQuery for ajax calls is one of the most sensible uses of jQuery, but it's better if you know how it works too.


Create pageC.php which should check if database has changed, and if so, output 1, else output nothing.

Then with jQuery and AJAX...

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <meta charset="UTF-8">
  <title>Quick AJAX test</title>
  <!-- needs to be url to jquery, can be locally, or via CDN -->
  <script type="text/javascript" src="jquery"></script>
  <script type="text/javascript">
    // this executes all contained after page has fully loaded
    $(function(){
      // define function to check for changed database
      function checkNew(){
        // make AJAX GET request, and set callback function
        $.get('./pageC.php',function(data){
          // check if the data returned (page content) is 1
          if(data === '1')
            alert('The database has changed')
          // run the function to check again
          checkNew()
        })
      }
      // call the checking function to initialise the loop
      checkNew()
    })
  </script>
</head>
<body>

</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜