将表单值存储在隐藏的输入数组中

时间:2018-03-29 13:06:43

标签: php html

我是php的新手,这是我的数字猜谜游戏的代码。由于我不知道如何定义一个随机数,我自己选择了数字,80。我试图在正确的数字之前存储所有的猜测,并在正确的猜测之后,在屏幕上打印出来。但我似乎无法把它弄好,因为它只打印出正确之前的最后一个猜测。

感谢任何帮助!

<html>
<head>
</head>
<body>
<?php 
$allguesses = array();
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $t = $_POST["guess"];
    $sayi = 80;

    if($sayi >$t){
        echo 'Guess higher';
    }elseif($sayi == $t){
        echo "You've guessed it right!<br>";
        echo 'Guessed numbers: <br>';
        foreach($_POST["tmn"] as $y){
            echo $y . ',';
        }
    }else{
        echo 'Guess lower';
    }
    array_push($allguesses,$t);
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php 
foreach($allguesses as $x){
    echo "<input type ='hidden' name = 'tmn[]' value=' ".$x . " '>";
}
?>
</form>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

对于你的学习曲线,会话似乎是最好的。

会话允许普通的无状态http协议记住每个表单提交之间的事情。因此,我们将在SESSION中的猜测数组中保存每个猜测,在PHP中也是一个数组。

<?php 
session_start();  // create a session, or reconnect to an existing one

if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guess"]) ) {
    $_SESSION['guesses'][] = $_POST["guess"];   // keep an array of guesses

    // $t = $_POST["guess"]; no need for extra variables on the stack
    $sayi = 80;

    if($sayi > $_POST["guess"]){
        echo 'Guess higher';
    }elseif($sayi == $_POST["guess"]){
        echo "You've guessed it right!<br>";
        echo 'Guessed numbers: <br>';
        foreach($_SESSION['guesses'] as $guess){
            echo $guess . ',';
        }
        $_SESSION['guesses'] = array();  // clear the old guesses out
    }else{
        echo 'Guess lower';
    }
}
?>
<html>
<head>
</head>
<body>
<form method="post">
    Guess the number:
    <input type="number" name="guess" min ="1" max = "100"><br>
    <input type="submit" name="submit">
</form>
</body>
</html>

答案 1 :(得分:0)

删除了不需要的代码。

使用以下代码更改显示先前猜测的方法。

echo implode( ", ", $_POST["tmn"] ); // cleaner 

此块处理将先前的猜测存储到用于显示先前猜测的数组中。

if( isset( $_POST ) ) {
        $_POST["tmn"][] = $t;
    }

修复了以下代码块的上一版本,以便正确输出之前猜测的隐藏<inputs> ..

<?php
    if( isset( $_POST["tmn"] ) ) {
        foreach($_POST["tmn"] as $x){
            echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
        }
    }
?>

更新代码:

<html>
<head>
</head>
<body>
<?php 
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $t = $_POST["guess"];
    $sayi = 80;

    if($sayi >$t){
        echo 'Guess higher';
    }elseif($sayi == $t){
        echo "You've guessed it right!<br>";
        echo 'Guessed numbers: <br>';
        echo implode( ", ", $_POST["tmn"] );
    }else{
        echo 'Guess lower';
    }
    if( isset( $_POST ) ) {
        $_POST["tmn"][] = $t;
    }
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php
    if( isset( $_POST["tmn"] ) ) {
        foreach($_POST["tmn"] as $x){
            echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
        }
    }
?>
</form>

</body>
</html>

答案 2 :(得分:0)

对于随机数,您可以使用rand($min, $max)。要存储猜测,您可以使用全局变量$_SESSION

<html>
    <head>
    </head>
    <body>
        <?php 
        //start session (needed to use the $_SESSION variable)
        start_session();
        if($_SERVER["REQUEST_METHOD"] == "POST"){

            //if empty -> initalize array
            if (empty ($_SESSION['allguesses']){
                $_SESSION['allguesses'] = array ()
            }

            $t = $_POST["guess"];
            $sayi = 80;

            if($sayi >$t){
                echo 'Guess higher';
            }elseif($sayi == $t){
                echo "You've guessed it right!<br>";
                echo 'Guessed numbers: <br>';

                //echo all guesses from $_SESSION variable
                foreach($_SESSION['allguesses'] as $y){
                    echo $y . ',';
                }

            }else{
                echo 'Guess lower';
            }

            //push in $_SESSION variable
            array_push($_SESSION['allguesses'],$t);
        }
        ?>
        <form method="post">
            Guess the number:
            <input type="number" name="guess" min ="1" max = "100"><br>
            <input type="submit" name="submit">
        </form>

    </body>
</html>