I have created a simple HTML form for submitting php code that updates my mysql table.
The problem im having is that the timer Can start by simply refreshing (providing its not already counting down) the page which i do not want. I only want it to start upon submitting the form.
Requirements needed for this timer: First execute the php code upon clicking submit button. Then disable the form submit button for 30 seconds and then re-enable after the timer has finished. This timer is saved to localstorage i believe so it is not interrupted(doesnt start over)by page refresh once it is already counting down which is good.
Here is a image of the feature im using the timer on: (https://i.stack.imgur.com/eWALf.jpg)
Here is the js timer code:
<script>
var time = 30;
var saved_countdown = localStorage.getItem('saved_countdown');
if(saved_countdown == null) {
var new_countdown = new Date().getTime() + (time + 2) * 1000;
time = new_countdown;
localStorage.setItem('saved_countdown', new_countdown);
} else {
time = saved_countdown;
}
var x = setInterval(() => {
var now = new Date().getTime();
var distance = time - now;
var counter = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("demo").innerHTML = counter + " s";
if (counter <= 0) {
clearInterval(x);
localStorage.removeItem('saved_countdown');
document.getElementById("demo").innerHTML = "Available";
}
}, 1000);
</script>
Here is how i have my html form setup:
<form action="" method="post">
<label for="players">Select your weapon:</label>
<select name="xWeapon">
<option value="" disabled selected>Choose option</option>
<option value="xKnife">Knife [FREE]</option>
<option value="xMachete">Machete [$15]</option>
<option value="xPistol">Pistol [$65]</option>
<option value="xMachine_gun">Machine Gun [$155]</option>
</select><br><br>
<input type="submit" name="link_to_php" value="Attempt">
</form><br>