Update $_SESSION[balance] automatically? - php

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

Related

Can you have a timer for SQL or PHP?

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.

Convert Static PHP/MYSQL Query to Dynamic One Using Ajax/jQuery

I have a counter that takes a sum from a database column and updates on every page load. I need to convert this to a live counter that updates without page load.
It's on a WordPress site, and I'm hoping not to have to create a separate page outside of WP in order to send the JSON array.
1) Is there any way to use the current page to submit/receive the POST data?
2) What's the best process for converting a simple 'SELECT * FROM' to something dynamic. Is ajax/jQuery the right choice?
Thanks!
Use the following to update the value every 5 seconds
window.setInterval(function(){
$.getJSON('/getLatestCount.php', function(data) {
$('#id_of_element_where_it_needs_to_be_put').html(data.count);
});
}, 5000);
if the query needs to have the contents of a form, use:
window.setInterval(function(){
$.getJSON('/getLatestCount.php', $("#my_form_id").serialize(), function(data) {
$('#id_of_element_where_it_needs_to_be_put').html(data.count);
});
}, 5000);
This doesn't use POST but GET but that shouldn't be a problem i think. In the http file use $_GET['my_field_name'] to access the form values.
You can output the json data in php through echo json_encode()
Looking at your question and that you're asking something for wordpress i'm not sure you know how to do this all though so i won't start going into depts about polling and websocket. This is the easiest way to do it.

Javascript getting count from db continuosly

Hi there Everybody!
I am facing a little issue.
I have a DIV where I need to show the count of rows from the database.
The problem is that when the page refresh the count is not still updated because the database mySQL takes a while, so I have to refresh the page again.
Do you know how can I show the count of the rows maybe with javascript? In a way that the count of the rows will be continuosly checked and updated without page reloading..
If I need jQuery for this, just to let you know I am on version 1.3.2
Let me know!
Thanks so much!
yea sure, ajax it... first, it does sound odd that when u refresh the page the database isnt ready, but lets assume there is a sync issue where db gets updated out of sync with the current page... the solution is ajax
function doCountUpdate(){
$.get("url_to_return_count",function(data){
assuming the returned data is a number
$("#myDiv").text(data);
setTimeout(doCountUpdate, 1000);
}
}
I think at page load, You can do that by simply a ajax call to a function and return the row and display it on a DIV.
$(document).ready(function() {
// put all your jQuery goodness in here.
$.ajax({
url: "somepage.php",
success: function(data){
$('#div_id').text(data);
}
});
});

Ajax call with MYSQL Update with a delay in the called page

Im currently working on a small search search script build with Jquery and AJAX. Its a live update script that dynamically loads the results in a div.
This is working perfectly but i want to keep track of what answers are being shown. I've tried putting an MYSQL update statement in the results page with a sleep() to prevent updates to the database happening to soon (some questions are shown for .5 seconds, those shouldn't be updated)
Im currently using the follow code:
sleep(5);
$id = $row_rs_results['vragen_id'];
$aantal = $row_rs_results['vraag_getoond'] + 1;
mysql_select_db($database_ruimerleven, $ruimerleven);
mysql_query("UPDATE vragen SET vraag_getoond = $aantal WHERE vragen_id = '$id'");
};
The problem with this is that it slows down the page to a crawl, anyone has a better solution for this? Thanks in advance!
Wouldn't it be better to have a backend process that you send the results to? That way you'd separate the update from the gui front end and it could flush the data to the database at it's leisure. The way your doing it, it would kill performance. Another option is to kick off another process that just does the database update and communicate with it.
I'm not exactly sure what you are going for, but it sounds like you are doing an instant result thing where it brings up results as you type? If that's what you are going for then I would suggest only submitting the AJAX call after the user has stopped typing for a certain amount of time. I would achieve this by putting the AJAX call in a setTimeout, and then every time the user types it resets the timeout.
Example:
var ajax_timeout;
$("#input").keydown(function() {
clearTimeout(ajax_timeout);
ajax_timeout = setTimeout(ajax_call, 500);
});
The php script should be able to update the database every time it gets called.

jQuery - PHP Database request each 5 secounds

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.

Categories