我制作了一个简单的程序,用户必须猜测虚拟骰子的值。
这是HTML和PHP:
<body>
<h2 id="heading"> We're gonna roll a dice! <br><small>And you have to guess which number the dice will roll.</small></h2>
<form action="" method="post">
<div id="test-container">
<p>Select your guess: </p>
<select class="form-control" id="select" name="dice">
<option value="">Select...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</div>
<button id="submit" type="submit" type="button" name="roll" class="btn btn-primary">Roll!</button>
<?php
if(isset($_POST['roll']))
{
$dice = $_POST['dice'];
$rand = rand(1,6);
if($dice == $rand)
{
echo "<br>" . "your guess is correct! The number rolled by the dice was " . $rand . "!";
}
else{
echo "<br>" . "your guess is wrong! The number rolled by the dice was " . $rand . " but the number you entered was " . $dice . "!";
}
}
?>
</form>
</body>
</html>
代码本身有效,但这不是问题。 当我按下提交时,我想要显示以下gif(位于按钮和回显输出之间),只播放一次而不循环:
我已经尝试了很多方法来做到这一点,但它们似乎都没有用!
据我所知,我需要一个图像编辑器来防止它永远循环。至于显示它,语法是echo "<img src='handroll.gif'>"
,正如我刚刚发现的那样。
答案 0 :(得分:2)
滚动骰子后,您是否尝试将$ _POST [roll]的值设置为null。正如我所看到的那样,一旦点击按钮并且你本身并没有“重新初始化”它,该值仍然是正确的。
if(isset($_POST['roll'])) {
// all that you need to do
// as the last line or something like this
$_POST['roll'] = null;
}
因为,通常在您提交表单时,表单数据或提交状态会被重置,会话值也会重置。
如果以上操作不起作用,您还可以尝试表单/输入类型=提交设置。通过jquery / js更轻松地点击按钮点击。