Php 将post表单值存储在隐藏的输入数组中
我是php新手,下面是我的数字猜测游戏代码。因为我不知道如何定义一个随机数,所以我自己选择了80。我试图在正确猜测之前存储所有猜测,在正确猜测之后,在屏幕上打印出来。但我似乎不能把它弄对,因为它只打印出正确猜测之前的最后一个猜测 感谢您的帮助Php 将post表单值存储在隐藏的输入数组中,php,html,Php,Html,我是php新手,下面是我的数字猜测游戏代码。因为我不知道如何定义一个随机数,所以我自己选择了80。我试图在正确猜测之前存储所有猜测,在正确猜测之后,在屏幕上打印出来。但我似乎不能把它弄对,因为它只打印出正确猜测之前的最后一个猜测 感谢您的帮助 <html> <head> </head> <body> <?php $allguesses = array(); if($_SERVER["REQUEST_METHOD"] == "POST"){
<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>
猜猜数字:
课程似乎最适合你的学习曲线
会话允许正常的无状态http协议在每次提交表单之间记住内容。因此,我们将在会话中将每个猜测保存在一个猜测数组中,在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>
猜猜数字:
删除了未编入的代码
用下面的代码更改了显示先前猜测的方法
echo implode( ", ", $_POST["tmn"] ); // cleaner
此块处理将以前的猜测存储到用于显示以前猜测的数组中
if( isset( $_POST ) ) {
$_POST["tmn"][] = $t;
}
修正了先前版本的代码块,以便正确输出先前猜测的隐藏
<?php
if( isset( $_POST["tmn"] ) ) {
foreach($_POST["tmn"] as $x){
echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
}
}
?>
猜猜数字:
对于随机数,您可以使用。要存储猜测,可以使用全局变量$\u 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>
使用$\u会话
或一些服务器端存储-数据库、文件等。非常感谢!非常感谢你@不客气。你介意接受我的回答吗?谢谢,这就是我一直在寻找的!
<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>