I'm trying to add 'guess' so that it shows in the text area without deleting the previous input. So that if you guessed '12' then '15' then '20'.
It would display in the text area as 12 15 20.
Rather than just displaying the most current value that you had entered for guess.
I've tried to put it into an array inside of the session, but the submit button is messing up the array when I try to display it inside of the textarea.
Any help would be greatly appreciated, thank you.
<?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>