views:

123

answers:

7

i need to change something in my site, if some fields changed in database, but without reloading the page! but i have no idea how i can do it. could you give me an idea? thanks

example:

for example, i have a forum, and i need to show a image, if new comment resieved! i can write all functions, but i don't understand when i must call the function? maybe window.onload?

+2  A: 

Ajax

Once you've familiarized yourself with the concept, you can reference the jQuery AJAX documentation for information on how to implement it with jQuery.

Matt
@Matt but how i know, Ajax doesn't solve auto refresh problem, isn't it?
Syom
@Syom the whole point of AJAX is to retrieve data without reloading the page ..
Matt
Ajax means „Asynchronous JavaScript and XML“ this is exactly what you need.
powtac
a litle example will be very useful
Syom
@Syom: You need to load some info from the web server if you want to keep the page up to date. There is no "push" technology that would let you move content into all browsers looking at a certain web page from the server side.
Adrian Grigore
@Adrian - Well, there are WebSockets ;) @Syom - AJAX is an immensely popular subject. Check out some of the many examples on jQuery's site.. like the [$.get()](http://api.jquery.com/jQuery.get/) page.
Matt
@Matt: True, there are websockets, but that's hardly a technology that I would use in a production system already.
Adrian Grigore
+2  A: 

Then you need AJAX!

Explanation:

You need two pages, the main page which does not "reload". And a second one which returns two versions of the small image based on the database field.

Page one has this JavaScript in it:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">

/* normal ajax without periodical refresh
$.ajax({
  url: 'page2.php',
  success: function(data) {
    $('#database_status').html(data);
  }
});
*/

var refreshId = setInterval(function() {
     $('#database_status').load('/path/to/your.php');
}, 3000);
</script>

// Or you use the jQuery plugin Heartbeat: http://www.jasons-toolbox.com/JHeartbeat/ 

And a div <div id="database_status">empty</div>

The second page, returns a image tag based on the database setting for example in PHP:

<?php
// do db request 
if ($request) {
    echo '<img src="true.gif"> TRUE';
} else {
    echo '<img src="false.gif"> FALSE';
}
powtac
A: 

The easiest way to perform an ajax request with jquery is the load method. On that page you can also see plenty of examples.

Adrian Grigore
A: 

There are two basic approaches you can take:

  1. Use setInterval to make an Ajax request periodically that ask the server if there are any updates.
  2. Use Comet to fake server push
David Dorward
+1  A: 

You can use jQuerys load- or JSON-method to get data from your server.

In your case the following scenario is possible:

  1. The site loads.
  2. Your JavaScript loads the initial data from the server.
  3. Now, every couple of seconds or minutes (Depending on your use case), the JavaScript asks the server if anything changed since it last asked (Use timestamps in your request, for example).
  4. If so, change the website accordingly.
  5. Goto 3.

Please keep in mind that excessive polling might but enormous strains on your server, especially if you have a lot of users. Long-polling or Comet are advanced techniques to handle the load.

christian studer
A: 

Try Ajax, e.g: using jquery which allows you to handle those in a high level js. example in this site when you do the send email

labadana