<html>
<body>
<p style='text-align:center;'>
<?php
$wordChoices = array("grape","apple","orange","banana","plum","grapefruit");//Course provided aray/code
if(isset($_POST['va']))
{
$randChoice=$_POST['va'];
print("Trigger CONTINUE");
}
else
{
$randChoice=rand(0,5);
print("Trigger New Rand");
}
//$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
}
print_r($_POST);
?>
</p>
<h1 style='text-align:center;'>Word guess</h1>
<div style='text-align:center'>
<a href='game.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='hid' value="">
</form>
<?php
if($_POST)
{
print_r($_POST);
$_POST['va']=$randChoice;
print("SET");
print_r($_POST);
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
}
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>
Simply put I need the randChoice variable to reset or get a new random after the correct value is guessed. If the incorrect value is guessed, it should remain the same. Any ideas? I am new so there may be things I have not considered, buit remember without SESSION or COOKIES being used