After a new data has been inserted into database, I want the page which is used to display the data from database to be refreshed/updated and show the new data automatically without hitting the refresh button (like in Facebook feed page, or right here in stackoverflow.com, when new answer posted, it shows an alert of that answer immediately) What techniques I should use to archive that?
Asked
Active
Viewed 1,801 times
2 Answers
5
There is a function in JavaScript called setInterval
that takes two arguments: a function to execute, and an interval in milliseconds with which the function is run. So, you can have a function called update
that fetches all the new data and appends it to your tables, and pass it to setInterval to continuously execute. To run an update function every 5 seconds, you can do something like this:
function update() {
//fetch new data using AJAX and update tables
}
setInterval(update, 5000);

Brennan
- 5,632
- 2
- 17
- 24
-
Oh OK, thanks for the response but I think I will be stuck at "//fetch new data using AJAX and update tables" most likely because I've used that function before to automatically refresh the page if user is idling, as in this post: http://stackoverflow.com/questions/4644027/auto-reload-web-page-if-there-is-no-recent-activity?lq=1 – Ronaldinho Learn Coding Sep 29 '14 at 21:54
-
Please explain the "//fetch new data using AJAX and update tables" do you have any good articles/ tutorial that I can follow? – Ronaldinho Learn Coding Sep 29 '14 at 21:55
-
@RonaldinhoState, The most common way of doing so is using jQuery's `$.get()`. A search into that should get you started. How exactly the fetching is done depends on your backend – Brennan Sep 29 '14 at 21:57
1
For make the page add new row to waiting customer table automatically
You have to make an ajax call in every few seconds to check whether any new row added if yes then you have to fetch that row and append it to table.

Aqib1604
- 292
- 1
- 7
-
Thanks for respond, I need more details/ explanation of your point please, my ajax background is very weak. – Ronaldinho Learn Coding Sep 29 '14 at 21:49
-
Ok So You have to make one php file which look into the database and fetch the new row if its there. and encode that row with json.In that php file you have to echo that encoded json. – Aqib1604 Sep 29 '14 at 21:56