我试图在我的主页 index.php 上显示一条警告消息。我使用第二个文件来插入按钮被按下的次数。但是用户应该只能每 5 分钟投票一次。如果投票失败或成功,它应该向他们显示一条警告消息。
现在它总是在 index.php 上显示两个按钮。
索引.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>
方法.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' );
?>
这是我目前所坚持的 fiddle 。 Fiddle (其实没必要)
它应该只在 5 分钟还没有过去时显示“等待 5 分钟”按钮和“成功!” 5 分钟后应显示按钮。如果用户没有点击“投票”按钮,则不应显示任何内容。
最佳答案
你永远不应该让前端决定你是否被允许投票。
永远不要相信用户输入,这里也不异常(exception)。您可以轻松地在您的 php 代码中添加一些检查:
$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 - 调用函数时在主页上显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846471/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL