user3510657 user3510657 - 1 year ago 92
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 Source

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:


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


$(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);
} else {
// timer is still above 0 seconds so decrease it
$("#button").data('time', newtime);
}, 1000);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download