php - Passing information using post method without session variables -


i admit homework. here last resort after cannot find suitable answer elsewhere. assignment having me pass information between posts without using session variable or cookies in php. user continues guess hidden variable carries on past guesses point. trying build string variable holds them , assign post variable cannot read off of guesscounter variable either undefined index error @ line of code should adding string variable or im not getting passed on @ all. here code appreciated have been @ awhile now.

  <?php      if(isset($_post['playerguess'])) {     echo "<pre>"; print_r($_post) ;  echo "</pre>";     }     ?>     <?php     $wordchoices = array("grape", "apple", "orange", "banana", "plum", "grapefruit");     $texttoplayer = "<font color = 'red'>it's time play guessing game!(1)</font>";     $therightanswer= array_rand($wordchoices, 1);     $passiton = " ";     $_post['guesscounter']=$passiton;     $guesstesttracker = $_post['guesscounter'];     $_post['theanswer'] = $therightanswer;     if(isset($_post['playerguess'])) {     $passiton = $_post['playerguess'];     if ($_server['request_method'] == 'get') {         $guesstesttracker = $_get['guesscounter'];          $therightanswer = $_get['theanswer'];     }     else if ($_server['request_method'] == 'post') {         if(isset($_post['playerguess'])) {             if(empty($_post['playerguess'])) {                 $texttoplayer = "<font color = 'red'>come on, enter something(2)</font>";             }             else if(in_array($_post['playerguess'],$wordchoices)==false) {                 $texttoplayer = "<font color = 'red'>hey, that's not valid guess. try again (5)</font>";                 $passiton = $_post['guesscounter'].$passiton;             }             if(in_array($_post['playerguess'],$wordchoices)&&$_post['playerguess']!=$wordchoices[$therightanswer]) {                 $texttoplayer = "<font color = 'red'>sorry ".$_post['playerguess']." wrong. try again(4)</font>";                 $passiton = $_post['guesscounter'].$passiton;             }             if($_post['playerguess']==$wordchoices[$therightanswer]) {                 $texttoplayer = "<font color = 'red'>you guessed ".$_post['playerguess']." , that's correct!!!(3)</font>";                 $passiton = $_post['guesscounter'].$passiton;              }         }     } } $_post['guesscounter'] = $passiton; $therightanswer=$_post['theanswer'];  for($i=0;$i<count($wordchoices);$i++){     if($i==$therightanswer) {         echo "<font color = 'green'>$wordchoices[$i]</font>";     }     else {     echo $wordchoices[$i];     }     if($i != count($wordchoices) - 1) {         echo " | ";         }     }  ?> <h1>word guess</h1> <a href ="">refresh page</a> <h3>guess word i'm thinking</h3> <form action ="<?php echo $_server['php_self']; ?>" method = "post"> <input type = "text" name = "playerguess" size = 20> <input type = "hidden" name = "guesscounter" value = "<?php echo $guesstesttracker; ?>"> <input type = "hidden" name = "theanswer" value = "<?php echo $therightanswer; ?>"> <input type = "submit" value="guess" name = "submitbutton"> </form>  <?php        echo $texttoplayer;     echo $therightanswer;     echo $guesstesttracker;    ?> 

this minimal functional example of need do. there still couple of minor bugs (like duplicate entries in history), i've left these exercise you. treat starting point , build need it.

i've added comments explain what's happening, clear you.

$answer = null; $history = []; $choices = ['apple', 'grape', 'banana']; $message = '';  // check if guess has been made. if (!empty($_post) && !empty($_post['guess'])) {     // check if previous guesses have been made.     if (!empty($_post['history'])) {         $history = explode(',', $_post['history']);     }      // check guess.     if (!empty($_post['answer']) && !empty($_post['guess'])) {         // check guess , answer both valid.         if (in_array($_post['guess'], $choices) && isset($choices[$_post['answer']])) {             if ($_post['guess'] == $choices[$_post['answer']]) {                 // correct; clear history.                 $history = [];                 $message = 'correct!';             } else {                 // incorrect; add history , set previous answer current.                 $history[] = $_post['guess'];                 $answer = $_post['answer'];                 $message = 'incorrect!';             }         } else {             // invalid choice or answer value.         }     } }  if (empty($answer)) {     // no answer set yet (new page load or correct guess); create new answer.     $answer = rand(0, count($choices) - 1); }  ?> <p>guess word i'm thinking:</p> <p><?php echo implode(' | ', $choices) ?></p> <form method="post">     <input type="hidden" name="answer" value="<?php echo $answer; ?>">     <input type="hidden" name="history" value="<?php echo implode(',', $history); ?>">     <input type="text" name="guess">     <input type="submit" name="submit" value="guess"> </form> <p><?php echo $message; ?></p> 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -