将值添加到文本区域

时间:2018-03-13 05:56:29

标签: php

我正在尝试添加'guess',以便它在文本区域显示而不删除以前的输入。所以,如果你猜到'12'那么'15'然后'20'。

它会在文本区域中显示为 12 15 20.

而不是仅显示您为猜测输入的最新值。

我试图将它放入会话内部的数组中,但当我尝试在textarea中显示它时,提交按钮会搞乱数组。

非常感谢任何帮助,谢谢。

<?php
session_start();

if (!isset($_POST["guess"])) {
 $_SESSION["AmountofGuesses"] = 0; 
 $message = "Guessing Game";
 $_POST["Answer"] = rand(0,1000);
} 

else if ($_POST["guess"] > $_POST["Answer"]) { 
$message = $_POST["guess"]." is too high, try guessing lower.";
$_SESSION["AmountofGuesses"]++; 
} 

else if ($_POST["guess"] < $_POST["Answer"]) { 
$message = $_POST["guess"]." is too low, try guessing higher.";
$_SESSION["AmountofGuesses"]++; 
} 

else { 
$_SESSION["AmountofGuesses"]++;
$message = "You've guessed the correct number in, 
".$_SESSION["AmountofGuesses"]." guess/guesses! Click restart to start a new 
game."; 
unset($_SESSION["AmountofGuesses"]);
}

if (isset($_POST["guess"])) {

$button= $_POST["button"];

$ArrayofNumbers = array();
array_push($ArrayofNumbers,$_POST["guess"]);

if ($button=="Restart"){
$message = "Guessing Game";
$_POST["Answer"] = rand(0,1000);
$_SESSION["AmountofGuesses"] = 0;
}

if ($button=="Answer"){
$message = "You've given up, your answer is above. Click restart to start a 
new game.";
$_SESSION["AmountofGuesses"] = 0;
echo $_POST["Answer"];
    }
}
?>

<title>Guessing Game</title>
<h3><?php echo $message; echo $ArrayofNumbers;?></h3>
<form action "program1.php" method="POST">
<table border="2" cellspacing="6">
<td>
<br/>
<p>Guess: <input type="text" name="guess"/> </p>
<input type="hidden" name="Answer" value="<?php echo $_POST["Answer"]; ?>" >
<p>Number of guesses: <?php echo $_SESSION["AmountofGuesses"]; ?> </p>

<center><input type="submit" name="button" value="Submit"></center>
<br/>
<center><input type="submit" name="button" value="Restart"></center>
<br/>
<center><input type="submit" name="button" value="Answer"></center>

</td>

<td>
<textarea name="paragraph_text" cols="50" rows="10">
<?php if (isset($ArrayofNumbers)) {echo implode("\n", $ArrayofNumbers);}?>
</textarea>
</td>
</table>
</br>

<a href="index.html"> Back to Home</a>

2 个答案:

答案 0 :(得分:0)

如果输出与12 15 20类似,那么您可以将$ArrayofNumbers更改为$PastGuesses,然后将每个下一个答案附加为常规字符串。

$PastGuesses .= " " . $_POST["guess"];

并确保$PastGuesses仍然存储在$_SESSION

答案 1 :(得分:0)

您可以使用数组的命令条件添加另一个新会话,如下所示:

  <?php session_start();
    if (!isset($_POST["guess"])) {
     $_SESSION["AmountofGuesses"] = 0; 
     $message = "Guessing Game";
     $_POST["Answer"] = rand(0,1000);
    }else if ($_POST["guess"] > $_POST["Answer"]) { 
    $message = $_POST["guess"]." is too high, try guessing lower.";
    $_SESSION["AmountofGuesses"]++; 
    }else if ($_POST["guess"] < $_POST["Answer"]) { 
    $message = $_POST["guess"]." is too low, try guessing higher.";
    $_SESSION["AmountofGuesses"]++; 
    }else { 
    $_SESSION["AmountofGuesses"]++;
    $message = "You've guessed the correct number in, 
    ".$_SESSION["AmountofGuesses"]." guess/guesses! Click restart to start a new 
    game."; 
    unset($_SESSION["AmountofGuesses"]);
    }

    if (isset($_POST["guess"])) {

    $button= $_POST["button"];
    if(@$_SESSION["guess"]!=''){
    array_push($_SESSION["guess"],$_POST["guess"]);
    }else{
    $ArrayofNumbers = array();
    array_push($ArrayofNumbers,$_POST["guess"]);
    $_SESSION["guess"]=$ArrayofNumbers;
    }

    if ($button=="Restart"){
    $message = "Guessing Game";
    $_POST["Answer"] = rand(0,1000);
    $_SESSION["AmountofGuesses"] = 0;
    unset($_SESSION["guess"]);
    }

    if ($button=="Answer"){
    $message = "You've given up, your answer is above. Click restart to start a 
    new game.";
    $_SESSION["AmountofGuesses"] = 0;
    unset($_SESSION["guess"]);
    echo $_POST["Answer"];
        }
    }
    ?>

    <title>Guessing Game</title>
    <h3><?=$message?></h3>
    <form action "" method="POST">
    <table border="2" cellspacing="6">
    <td>
    <br/>
    <p>Guess: <input type="text" name="guess"/> </p>
    <input type="hidden" name="Answer" value="<?=@$_POST["Answer"]?>" >
    <p>Number of guesses: <?=@$_SESSION["AmountofGuesses"]?> </p>

    <center><input type="submit" name="button" value="Submit"></center>
    <br/>
    <center><input type="submit" name="button" value="Restart"></center>
    <br/>
    <center><input type="submit" name="button" value="Answer"></center>

    </td>

    <td>
    <textarea name="paragraph_text" cols="50" rows="10">
    <?php if (@$_SESSION["guess"]!='') {
        foreach ($_SESSION["guess"] as $value) {
        echo $value."\n";
    }
    }?>
    </textarea>
    </td>
    </table>
    </form>