Ajax Auto Refresh – Volume II
So here’s the deal. More than two years ago i wrote a post about auto refresh content after changes in database – AJAX. Although my mainly goal was to share with you the concept behind it, i don’t think it’s enough for some people ;o) So here it is. Auto refresh volume two with the working online example and based on jQuery.
JS library
This time i decided to go with jQuery. Mainly because I started to use it on daily bases and because prototype isn’t that popular anymore. I liked it as a nice and smaller alternative for jQuery, but let’s be honest. If you need something smaller than jQuery, use plain javascript. Otherwise go with jQuery ;o) But this is my personal opinion.
Volume Two
If volume two. then there should be more changes. The concept is still the same. If you’re curious about it or you just want to compare how lame I was two years ago, go ahead and check old post. I won’t delete it, but i don’t want to confuse you more. That’s why i wrote this post ;o)
The new code is more elegant and more ‘pro’ (fuck yeeeaaah). I even used only one ajax request instead of two. I also choose to use JSON and data attribute. That way no extra markup is needed for this purpose.
Working example
Some of you might say ‘finally’. I don’t blame you, but you know – people must eat, and buy food for that. With money. That suck, right? Anyway, go to online demo to see this beauty in action. In the second window open this small form with text input. Resize both windows so they would fit on your screen and you could see changes. Add something with that form and wait. Don’t refresh the page with message list. Whole idea is for that page to refresh list of messages automatically if there are some changes. Just wait 20 seconds to see your message. Just don’t add some stupid things. You can add funny message or the one like ‘i love you’, ‘you’re my hero’ and so on. It’s just a demo right? ;o)
Source code
You can download . The package contain .sql file with table you need to create in order to get it work. Also in db.php you need to provide data for database connection. In index.php you can find JS function and html structure of demo. Nothing more really. Common.php is just added because 3 files seems too few ;o) That’s not true of course. I just didn’t want to repeat myself. Remember, it’s a demo. Just to show you that idea from two years back really works ;o) any errors, stupid mistakes, absence of mysqli close and so on are because it’s a demo of concept. Just adjust this concept with your needs and requirements.
The db.php
In this file you can find all functions I needed to talk with database. There are two very important functions. Rest are added for demo. Check_changes and register_changes are the functions that creates magic ;o) everytime you add, edit or delete content, you need to register that change. You can’t really rely on row count in table, cause with edition (which is a change of content) the row count is the same. That’s why there is this counter. You can move it to another table or whatever. Just update it when there is a change.
The checker.php
This is the file which will give us result of ajax request. The ajax call is made. Checker checks if current counter is diffrent from the one in database, if yes it selects new list of messages. Current value of counter is send with the ajax request, and stored in data-counter attribute of div#message-container. When user opens the page for the first time, don’t forget to set data-counter with current value from database.
Final thoughts
This should give you a place to start. You should modify the script based on your requirements. For example if you need refresh list of messages that cannot be edited or deleted you don’t need a counter. In check_changes you can just check row count in table, and that row count can be your counter. Count, counter, counting… Yeah that’s it for today. Good luck!
Nice post, very informative but the demo site is down. Can you bring it back up?
Demo is back online :o)
Hi, Eliza :)
How can i apply your code to a laravel way? TIA!
Well you need to change queries to native Laravel queries and use probably some controller function to pull data via ajax. Never worked with laravel – only with codeigniter and symfony, but as I remember there is similar MVC concept with routes, queries and so on. Since it’s been 6 years (holy molly) since I’ve written this post, today instead of making “counting” column to keep track of changes I would simply use add_date and edit_date columns that you will probably have in your database anyhow. Since this post/concept is very popular, I might revisit it next year and use… Read more »
the demo is nice, thank you. ^^
Please help me!
You say: “When user opens the page for the first time, don’t forget to set data-counter with current value from database.”
I do not know what to change and where in the checker.php file.
Thank you
How to insert with ajax without refreshing page !
is that just re call ajax with timeout?
how can i implement this code in wordpress page. I made a new Page Template and data is showing in wordpress page. but its not refreshing. How to add Jquery to refresh wordpress page ?
Your code works great, but it does not work when updating an existing database entry. It only registers new entries.
You can add another id tag for those entrys
How could I get more than one select result using it?
For exemple, i’d like to get two select from two different databases, but I don’t know how do it using jquery, I just know to get the information that I want, but in the checker.php and index.php I don’t know to to proceed. Can you help me?
Hello Eliza, I’m using your script for a chat application. I need to pass PHP variables to the database queries. It’s working great except for one line of code, I’ve worked on it for several days. Would you mind looking at my stackoverflow post? http://stackoverflow.com/questions/34544208/ajax-auto-refresh-php-variables-not-passing-correctly-into-auto-refresh-functi
RamRaider on StackOverFlow gave a great reply, thanks
Your live demo is down!
Oopsy, i forgot to move it! Thanks for letting me know. It will be up and running in a bit.
Hey, no problem. That’s a nice piece of code.
I’ve tried to incorporate it into a notification system, can you help with this:
http://stackoverflow.com/questions/33918535/combining-jquery-ajax-and-php-trouble
Hi again, thanks for your help on StackExchange – I have the notification triggering now, but it always displays the previous available result!
In the notification line i have get_news();?>, which should do it, right?
Thanks for your time – I’ve taken up more than i should already!
can i ask something this sample is sqlite base am i right? how to implement this with mysql? im new in php sorry
Hi Eliza, Great work!
I need to do something similar, but i can't change my DB to include the "counter" column. I'm trying to show new posts from WordPress when new post is added to DB (I don't need to control the posts when its updated, just need to list the newest and delete from list when its errased from DB). There's a way to modify your code to do it, based on the post time? Can you help me ?
Well my problem was simply of wordwrap. Luckily its solved. But it would be great if there were some text to emoticon conversion. I am not that great at php but I tried my hand at this improvement. But it has some glitches. I would really like to share with you. Is it possible somehow?
sorry can i ask where is the function called? __construct()
i know its needed but i cant seem to figure out how they get the data without calling the function
thanks
db.php
class db{
/**
* db
*
* @var $ public $db;
*/
public $db;
/**
* __construct
*
* @return void
*/
function __construct(){
$this->db_connect('localhost','root','password','test');
}
ayan, if you have some kind of error that says "memory/limit exceeded" or sth like that, then you have a php error. You probably need to increase value for memory_limit or post_max_size in php.ini.
Its really great but it has 1 problem. Large texts exceed page boundaries. How could i solve that?
Great Job Dear
Awesome tutorial ,, keep rocking..
Hi, index page does not show anything.
when i check the src code it stoped at
<div id="message-list" data-counter="
where in index.php it says <div id="message-list" data-counter="<?php echo (int)$db->check_changes();?>">
what i am doing wrong ?
Great Work. Thank You
Great work. I think how to display message in Codeigniter footer. Message I need is: is DNS IP addr dead? I can do check DNS IP addr with php code. I can put the status into table cell ( 0 or 1). Every xxmin I can refresh this status. But how to display only this message, and not refresh the other screen data ( header, main view and other part of footer)? I tried to do it with jquery code from your site using:function my_function(){ $('#refresh').load(location.href + ' #time'); but this function refresh me whole page! If I at the… Read more »