繁体   English   中英

调用函数时在主页面上显示警报

[英]Display alert on main page when function is called

我正在尝试在我的主页上显示一条警告消息,即index.php。 我使用第二个文件来插入按下按钮的次数。 但是用户应该只能每5分钟投票一次。 如果投票失败或成功,它应该显示警告消息。

现在它始终显示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' );
?>

这是我目前所困扰的小提琴。 小提琴 (它不是真的有必要)

它应该只显示5分钟尚未过去的“等待5分钟”按钮和“成功!” 5分钟过后,应显示按钮。 如果用户没有单击“投票”按钮,则不应显示任何一个。

你永远不应该让前端决定是否允许投票。
永远不要相信用户输入 ,这里也不例外。 您可以在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;

要改进UI,您可以添加计数器。 只需将您的消息更改为以下内容:

$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基于这个主题的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM