ページをリロードまたは送信すると、推測される単語が$ guessesに追加され、パイプが追加されます。リロードするたびにreplaceを追加する必要があります。ページの読み込みを維持するために隠し要素に格納していますが、動作していないようです。私は間違って何をしていますか?変数を連結しないのはなぜですか?
<html>
<body>
<p style='text-align:center;'>
<?php
$wordChoices = array("grape","apple","orange","banana","plum","grapefruit");//Course provided aray/code
$randChoice;
$tempVar;
if(isset($_POST['gu']))
$guesses=$_POST['gu'];
else
$guesses="";
echo "|||||||||" . $guesses . "||||||||";
if(empty($_POST['va']))
{
$randChoice=rand(0,5);
}
else
{
$randChoice=$_POST['va'];
}
//$randChoice = rand(0,5);//NEEDS FIX
for($i=0;$i<=5;$i++)//output of array and green text
{
if($i==$randChoice)
echo "<span style='color: green; text-align:center;'>$wordChoices[$i]</span>";//prints green word
else
echo "<span style='text-align:center;'>$wordChoices[$i]</span>";//print word
if($i<5)//kills extrta "|"
echo " | ";//print break between words
}
?>
</p>
<h1 style='text-align:center;'>Word guess</h1>
<div style='text-align:center'>
<a href='lab1.php'>Refresh This Page</a>
</div>
<h3 style='text-align:center;'>Guess the word I'm thinking</h3>
<form action="<?php $_SERVER['PHP_SELF'];?>" method='POST' style='text-align:center;'>
<input type='text' name='tb1' placeholder=<?php echo $wordChoices[$randChoice];?>>
<input type='submit' name='butt'>
<input type='hidden' name='va' id='va' value="<?php echo $randChoice;?>">
<input type='hidden' name='gu' id='gu' value="<?php echo $guesses;?>">
</form>
<?php
if($_POST)
{
if(!empty($_POST['tb1']))
{
$guesses += $_POST['tb1'] . "|";
}
if(isset($_POST['tb1']) && $_POST['tb1'] == $wordChoices[$randChoice])//checks to see if correct
echo "<p style='text-align:center; color:red;'>You guessed " . $wordChoices[$randChoice]. " and that's CORRECT!!! (3)</p>";//prints out correct
else if(!isset($_POST['tb1'])||$_POST['tb1']=="")//checks if nothing entered
echo "<p style='text-align:center; color:red;'>Come on, enter something (2)</p>";//prints out message to enter text
else if(isset($_POST['tb1']) && isValid($_POST['tb1']) && strtoupper($_POST['tb1'])!=strtoupper($wordChoices[$randChoice]))//checks for valid guess but incorrect term
echo "<p style='text-align:center; color:red;'>Sorry " . $_POST['tb1'] . " is wrong. Try again (4)</p>";//prints out valid incorrect guess
else if(isset($_POST['tb1']) && !isValid($_POST['tb1']))//checks if guess is valid
echo "<p style='text-align:center; color:red;'>Hey, that's not even a valid guess. Try again (5).</p>";//prints out invalid
echo "<ul>".$guesses."</ul>";
}
else
echo "<p style='text-align:center; color:red;'>It's time to play the guessing game! (1)</p>";
function isValid($textResp)//checks is guessed word is a valid word
{
print($textResp);
$wordChoices = array("grape","apple","orange","banana","plum","grapefruit");
if($textResp==$wordChoices[0])
return true;
else if($textResp==$wordChoices[1])
return true;
else if($textResp==$wordChoices[2])
return true;
else if($textResp==$wordChoices[3])
return true;
else if($textResp==$wordChoices[4])
return true;
else if($textResp==$wordChoices[5])
return true;
else
return false;
}
?>
</body>