php 提交一次表单rand如果不覆盖之前的?
本文介绍了php 提交一次表单rand如果不覆盖之前的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
<?php
$s1 = rand(1,9);
$s2 = rand(1,9);
if($_POST['daan']){
if($_POST['daan'] == $s1+$s2){
$txt = '正确';
}else{
$s = $s1 + $s2;
$txt = '错,正确答案是' .$s;
}
}
?>
<form id="form1" name="form1" method="post" action="">
<?=$s1?> + <?=$s2?> = <input type="input" name="daan" />
<?=isset($txt) ? $txt : '请回答'?>
<input type="submit" name="button" id="button" value="提交" />
</form>
代码如上,现在的情况是提交表单后,rand值为提交后的值,造成答案错误,请问如果做才能不覆盖上次的rand值呢?
解决方案
session_start();
if($_POST['daan']){
if($_POST['daan'] == $_SESSION['result']){
$txt = '正确';
}else{
$txt = '错,正确答案是' .$_SESSION['result'];
}
}else{
$s1 = rand(1,9);
$s2 = rand(1,9);
$_SESSION['result'] = $s1 + $s2;
}
这篇关于php 提交一次表单rand如果不覆盖之前的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文