Display a warning on the main page when calling a function

I am trying to display a warning message on my main page, which is index.php. I use the second file to insert the number of clicks on the button. But the user should be able to vote only every 5 minutes. He should display their warning message if the vote failed or succeeded.

Now it always displays both buttons on index.php.

Index.php

<html>
    <body>
        <!-- ** PHP ** -->
        <?php 
        include_once 'resources/method.php';
        ?>
        <!-- ** HTML ** -->
            <form id="button1" action="resources/method.php" method="post">
                <input type="submit" value="VOTE"/>
            </form>
    </body>
</html>

method.php

<?php
// if (5mins passed) {
// } elseif (time <= 5mins) {
//  insert query;
// }

// if (5mins passed) {
?>
    <div class="alert">
        Wait 5mins
    </div>
<?php
// } else {
?>
    <div class="alert">
        Success!
    </div>
<?php
// }
?>

<?php
header ( 'Refresh: 5; URL=/project/index.php' );
?>

Here is the scenario of what I'm stuck with right now. Fiddle (this is optional)

" 5 ", 5 , "!" , 5 . "", .

+4
1

, .
, . phpcode:

$time2wait = 300;
if( isset($_POST) ){
    if( $_SESSION['last_action']+$time2wait > time() ){
        $message = "Sorry, only one vote per ".$time2wait." seconds";
    }
    else{
        // Do you saving stuff
        $_SESSION['last_action'] = time(); // store last action
        $message = "The action is complete";
    }
}
/* other code might go here, or some html, or some templates */
echo $message;

, . :

$message = "Sorry, only one vote per ".$time2wait." seconds";
$message.= "Please wait <span id='counter'>".($_SESSION['last_action']+$time2wait-time())."</span> seconds";

. , javascript:

var count = parseInt(document.getElementById("counter").innerHTML, 10);
function timer(){
    count=count-1;
    if (count <= 0){
        clearInterval(counter);
        // Refresh your page here, like: window.location=window.location;
        return;
    }
    document.getElementById("counter").innerHTML=count;
}

var counter=setInterval(timer, 1000); //1000 will  run it every 1 second

- javascript

0

Source: https://habr.com/ru/post/1584473/


All Articles