Bill
Bill

Reputation: 5668

Ajax or jQuery page reload with new data added to sql database

I'm totally new to ajax, but pretty good with PHP. I have a PHP page which currently refreshes every 2 minutes because it has to check for new data in the sql database. It's pretty annoying if you're using it and it keeps refreshing. The database is updated about 3 times a day, but what is shown on that page needs to be kept current with what's in the database. I'm trying to find a way to have ajax or something check the database every 30 seconds or so, and if there is something new, then refresh, but if there isn't anything new don't refresh. Does anyone know of any tutorials out there, or something that can get me started? I have the concept down I think... I just can't figure out the code. I'm sure if I get it to check the database I can figure out the rest.

Thanks in advance!

Upvotes: 1

Views: 1865

Answers (1)

kijin
kijin

Reputation: 8910

Check out jQuery. It makes it super easy to retrieve a remote resource in the background: $.get(url, callback). The contents of the page should be in either XML or JSON. I suppose you already know how to generate these using PHP.

Once you've retrieved your updates (or lack thereof), you can decide what to do with it. Again, with jQuery it's very easy to append to or replace the contents of any part of the page, without ever refreshing anything. $("#id").html(new_html)

And that's just the beginning. You can do a lot more than that. Welcome to the world of AJAX!

Of course, you can do AJAX with vanilla JavaScript. But then you have to account for all the different browser perks, so I highly recommend using a framework such as jQuery. It also comes with a pretty comprehensive online documentation and some examples, so try them out.

Upvotes: 3

Related Questions