user3510657 user3510657 - 5 months ago 9
SQL Question

Show/disable button with PHP and JavaScript

How would I go about disabling/enabling a button when the UNIX timestamp reaches <= 0.

I use AJAX to continuously call a .php-file every 250ms. In this .php-file I check if its been 90 seconds since last action was taken by comparing the time (in UNIX) now to the last time I pressed the button (using a MySQL database).

The comparison is done by subtracting time it was pressed to the time now.

I would like to enable the button when the UNIX timestamp when there's less than or equal to 0 seconds since the button was last pressed.

Having a cooperation between PHP and JavaScript is a challenge, I know that, but I also know there are ways to do this.

Thanks in advance.

Answer

You could do this with jquery depending on what the script is about. Rather than calling ajax, you could assign an attribute to the time and then using a jquery setInterval function decrease the time by 1 second and then check whether the time is less than 0 or not? Example:

HTML:

<input type='submit' id='button' name='button' data-time='(using php, put the seconds in here' />

Jquery

$(document).ready(function() {
var timer = setInterval(function() {
var current = $("#button").data('time');
// get the current value of the time in seconds
var newtime = current - 1;
if (newtime <= 0) {
// time is less than or equal to 0
// so disable the button and stop the interval function
$("#button").prop('disabled', true);
clearInterval(timer);
} else {
// timer is still above 0 seconds so decrease it
$("#button").data('time', newtime);
}
}, 1000);
});
Comments