jaArch Posted October 2, 2011 Share Posted October 2, 2011 Hi: I need help with a PHP form - for some reason I can't seem to get the allTheGuesses part to appear in the r_post section when the user enters more than one guess. It should display all the guesses the user has entered in 1) a list below the form and 2) in the r_post at the top of the page. It's only displaying the most recent guess - which is in the myguess form....it should also display the latest guesses. An example: myguess => lol allTheGuess => lol | loll | lolll That's what it should look like...this is what it's looking like for me myguess => lol allTheGuesses => ^^ That part is just blank Here's my code: <pre><? print_r($_POST) ?> </pre> <? $WORDS = array("grape","apple","orange","banana","plum","grapefruit"); define ("GUESSINDEX",3); define("THEWORD",$WORDS[GUESSINDEX]); foreach ($WORDS as $val){ if ('banana' == $val) echo 'found it.'; else echo 'not found.'; } if (in_array('plum',$WORDS == true)){ } foreach ($guessesarr as $val) echo "<li> $val</li>"; foreach ($WORDS as $val) echo "$val | "; echo "<br>"; echo implode(' | ',$WORDS); ?> <html> <style type="text/css"> body {background-color:orange} div {margin:10px} div#container {background-color: white; border: 3px solid black;width: 400px;margin-right: auto;margin-left: auto; text-align:center;} div#error {color:red} div#guesses,div#header p span {color:green; } h1,h2,h3 {color:#633} </style> <body> <div id="container"> <div id="header"> <p>grape | apple | orange | <span>banana</span> | plum | grapefruit</p> <h1>Word Guess</h1> <a href="<?$_SERVER['PHP_SELF']?>">refresh this page</a> </div> <div id="form"> <h3>Guess the word I'm thinking</h3> <form name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>" > <input name="myguess" type="text" value=""> <input type="hidden" name="allTheGuesses" value = ""> <input type="submit" value="GUESS"> </form> </div> <div id="error"> <? if ($_SERVER['REQUEST_METHOD'] == 'GET') echo "It's time to play the guessing game! (1)"; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $MYGUESS = trim($_POST['myguess']); if ($MYGUESS == "") echo "C'mon, guess something (2)"; elseif (in_array($MYGUESS,$WORDS) == false) echo "Hey, thats not even a valid guess. Try again (5)"; elseif ($MYGUESS == THEWORD) echo "You guessed " . THEWORD . " and that's CORRECT!!! (3)"; elseif ($MYGUESS <> THEWORD) echo "Sorry $MYGUESS is wrong. Try again (4)"; } ?> </div> <div id="guesses"> <center> <? echo "<ol>"; $allguesses = trim($_POST['allTheGuesses'],", "); $guessarr = explode(", ", $allguesses); foreach( $guessarr as $guess ){ echo "<li>$guess</li>"; } echo "</ol>"; ?> </center> </div> </div> </body> </html> This is what I need to do: Word Tally - note that a tally of the all the guesses are printed at the bottom of the page as an ordered list. A hidden form variable is used to store all the previous guesses. (no cookies, no session variables) Some functions I used: empty() to determine if a variable contains an empty string. strlen() to determine the length of a string. in_array() to determine if a value is in array split() and join() to convert a string into an array and vice versa foreach($array as $val) to walk through an array Any help with this would be appreciated! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/248268-php-form-help/ Share on other sites More sharing options...
Buddski Posted October 2, 2011 Share Posted October 2, 2011 It is showing up blank because it has no value. <input type="hidden" name="allTheGuesses" value = ""> Quote Link to comment https://forums.phpfreaks.com/topic/248268-php-form-help/#findComment-1274957 Share on other sites More sharing options...
jaArch Posted October 2, 2011 Author Share Posted October 2, 2011 That's what I thought and that's where I'm stuck...what am I supposed to put in as a value? $_POST and something like that? Quote Link to comment https://forums.phpfreaks.com/topic/248268-php-form-help/#findComment-1274998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.