I want to do a Database request each 5 secounds. And if there is a new DB insert, I want to reload a Div!
My brain is burning I can't doing this without a tipp. Please help me
How about this...
Have a table that contains the "last update" date and nothing else. Then you can perform a very small query to see if that date has changed and then reload the page.
So when you insert (or update if you are worried about those too) you update the date in the last update table.
Your jQuery call should be a get request to a page that just outputs the date.
You compare the date with the one you obtained originally and if it has changed, reload.
$.get("http://yoursite/lastupdate.php", function (data) {
if (data != originalDate) {
document.location.reload();
}
});
store the date/time the page was loaded into a javascript variable. use setTimeout to run an ajax request every 5 seconds, passing the datetime var. return any rows added after this time. if any rows are returned, update the variable and the div.
PeriodicalUpdates
You could try to use Jquery's PeriodicalUpdates plugin from Github.
Related
So i've found options but they're not suitable. Im currently making a PHP browser game which uses an SQL server.
Im trying to figure out a way for when the button is pressed, after 300 seconds an SQL cell should update and during the 300 seconds for it to be displayed on screen. Doesn't have to be a count down, even whats left.
Figuring out a way to do the 300 seconds and have it displayed when refreshed is what im having trouble with.
If you can get away with the potential problems that may arise if the user closes your application's browser tab, your best option would be a JavaScript snippet to do it:
When the user clicks the button, lunch a JavaScript timeout like this
// 1. prepare timeout
setTimeout(on_timeout_fn, 300000);
What this piece of code does is, after 300 seconds, it will call on_timeout_fn function.
When time is up, your function on_timeout_fn will execute. You should define it to something like this:
let on_timeout_fn = function() {
// 2. run an ajax call to execute a PHP script that will update the database
// if you're using something like JQuery, it should be as easy as:
$.post("update_db.php", "data_to_send", function() {
// 3. when ajax call ends, make sure the button dissapears
$("#yourButtonID").toggle();
}, "text");
}
Well you would have to do this with JavaScript, and PHP. You can also use JQuery to do this feature. In Javascript you would grab the button on by its query selector wether that be an class or id by clicking and then you could set an a timer to where it won't execute the code until 300 seconds, then it runs the code sends the value to the back end by AJAX, fetch whatever you prefer. Then proceeds to use use PHP and MYSQL to update the data.
No, it doesnt exist really, not in 'standard' code. You can aproach it some other way though:
On click you update the database, you set an eg timerReady to NOW()+300sec.
On refresh/reload/cron/whatever you check if timerReady < NOW(), then you know the 300sec have past. Otherwise, timerReady - NOW() is the amount of second remaining.
When you press the button you let javascript wait 300sec (I do recommend a timer/indicator) and then reload. And when the user reloads in the meantime, you can use the timerReady - NOW() trick to decide how long the user has to wait still.
You can do this with Javascript only, but if you do not store it in the database, when a user refreshes they must start over as you have no way of verifying anything.
I have a page that uses ajax to show users their current assignments. Instead of having to refresh the page to see if there are any updates, I'm using ajax to update the data every 4 seconds. It's been requested that I change the document title to show something like "Number of Tasks: 4" and have that update as well when the user either completes a new task, or gets assigned another one. I tried using a simple "setInterval" javascript function, but since PHP is server side, the variable piece doesn't update...
I've also tried setting "document.title" from within the ajax code, but that just plain didn't work.
Is there a simple way to update the document title to show the number of tasks assigned to the user viewing the page?
Return the value from the $_SESSION in the data sent with the AJAX response to the client Javascript code. Once you have it on the client side set whatever you need to it with javascript.
You'd have to call with ajax a php dedicated to return you only the number of tasks (and other information you may want).
To change the title you can just call document.title = "the data returned in ajax";.
And put all this code (ajax call and title set) inside a function with setinterval as you mentioned.
I have a button and I want this to call a event via AJAX. This event is for delete a DB record so I want the content updated without the delete value/row. I think maybe I should put my code in the success of the .ajax call but my doubt goes to how to refresh the content? Do I need a second AJAX call to get the new content without the deleted value/row?
Thanks in advance
If you are using ajax then why are you refreshing page ? We use ajax to avoid page refresh. Still if u need it u can try add window.location.reload(); function in your ajax.success() function. here is a example in case its useful to you http://www.amitpatil.me/ajax-table-adding-removing-rows-dynamically-using-javascript-animation/
You can have it in the same call. In the DELETE script that the ajax call is pointing to just add on the query and send the data back using json_encode().
Then you simply update that specific area of the page.
Alternatively if you want to be lazy, after a success return from the ajax, simply do:
window.location.reload();
which will refresh the page.
If you use AJAX for delete records than you don't need reload all page content (it's a goal of AJAX). In this case you must change you content with JavaScript DOM element removing.
If you delete some record with AJAX with some id you performe something like:
$("#deleteBtn").click(function() {
// detect ID of the record (var id = ...)
// call AJAX with post(...) or ajax(...)
$("#id" + id).remove();
});
Or better if you perform remove() function on AJAX result for more security and remove it only if your server side script really have delete the record from DB.
In my example assumed that you use id attribute of the your rows (or other HTML elements) for storing id of the record for access in future via $("#id" + id). You need some entry point for find your DOM element by record id.
As I see it, there are who ways of doing this.
1: wrap your rows in a container with the unique ID from the database row, and then use that ID to remove/hide the row that you just deleted from your page.
2: as you mentioned, you can fetch the data again and print it, but this won't be very efficient performance-wise.
/regards
I get the value of $_SESSION[balance] from a MySQL database on every login. How can I update the value in clients browser without reloading the page every 5 minutes? I think it can possibly be done using AJAX?
Sorry if that's too vague I'm absolutely clueless as to where to start on this.
your're right, you need AJAX for this. easiest way is to use $.get(); with the jQuery library
js/jQuery script
window.setInterval(function() {
$.get('script.php', function(balance) {
$('#balance').html(balance); // set the value to the element with the ID balance
});
}, 60000); // execute every minute
in script.php you simply query the database for the balance and echo it
I've a page that show data inserted in a mysql db; I'd like (if it is possible) that this view page automatically update when a record is added to the db (data are not inserted from the same page or may be inserted from another user).
thanks in advance
ciao h.
First way (easiest but annoying):
Add a meta refresh to the page and the full page refreshes every x amount of time.
Second way (AJAX):
I'm assuming this is what you want.
Load default HTML page etc... lets call it index.php
On above page create a <div> element with style display:none like this
<div id="mydata" style="display: none;"></div>
Create a PHP page that retrieves and formats the database data and prints it out in x format, i.e a table or p tags. no headers or page specific stuff just data and simple tags
On index.php put something like this
<script type="text/javascript">
function update() {
$.ajax({
url: 'http://www.example.com/getData.php?q=latest',
success: function(response) {
$('#mydata'.html(response);
}
});
}
// .ready function
$(function() {
// do setup stuff here, I think there is a refresh timer or something
// that you'd use to trigger it every x amount of time
// i.e something like this
update();
});
</script>
EDIT: AJAX HTTP streaming forces data out to users
I know sure on the best method to do this, but and quick-and-easy solution would be to get the total number of rows in your table on page load. Then every x seconds use an AJAX script to re-check that table's row count. If it has changed you could have extra logic that then fetches the new rows.
I was thinking about this last night and realized that node.js (http://nodejs.org/) might be perfect for this if you're looking for an event driven system. There is a good tutorial on getting started using node.js at http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/.