Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提交时停止洗牌数组,但刷新页面时继续洗牌数组_Php_Arrays - Fatal编程技术网

Php 提交时停止洗牌数组,但刷新页面时继续洗牌数组

Php 提交时停止洗牌数组,但刷新页面时继续洗牌数组,php,arrays,Php,Arrays,我需要在每次刷新页面时洗牌一个数组,但我需要提交最后一个结果。在我的例子中,数组在提交时一直在洗牌 $a = array("test1","test2","test3"); shuffle($a); $result = $a[0]; 示例$result=“test2” 这是我的密码 if($_POST["submit"]) { //when do submit $result is shuffling again if($_POST["input_text"] == $resul

我需要在每次刷新页面时洗牌一个数组,但我需要提交最后一个结果。在我的例子中,数组在提交时一直在洗牌

$a = array("test1","test2","test3");
shuffle($a);
$result = $a[0];
示例
$result=“test2”

这是我的密码

if($_POST["submit"]) {
   //when do submit $result is shuffling again

   if($_POST["input_text"] == $result) {
       echo "true";
   }else {
       echo "false";
   }
}

<form method='POST'>
    <input type="text" name="input_text">
    <input type="submit" name="submit" value="submit">
</form>
if($\u POST[“提交”]){
//什么时候提交$result再次洗牌
if($\u POST[“输入文本”]==$result){
呼应“真”;
}否则{
呼应“假”;
}
}

实际上,我可以将结果传递给input type hidden,但我想防止使用input type hidden方法。

您可以在初始加载时单独保存结果。您可以从使用会话开始:

<?php
// start the session
session_start();

function shuffle_array() {
    $a = array("test1","test2","test3");
    shuffle($a);
    $result = $a[0];
    $_SESSION['result'] = $result;  
}

if(!empty($_POST["submit"])) { // submitted

   if($_POST["input_text"] == $_SESSION['result']) {
       echo "true";
       // if correct, shuffle again
       shuffle_array();
   } else {
        // try until you get it correct
       echo "false";
   }

} else {
    // not submitted or initial
    shuffle_array();
}

?>


是否要在提交时保存
$result
的状态?只需使用会话即可,只需在进行比较后将其取消设置我已尝试使用会话。。如果我刷新页面,数组将不再洗牌。。刷新页面时我需要洗牌@鬼魂