Jump to content

PHP form help


jaArch

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/248268-php-form-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.