2014-09-23 20 views
0

我会立即承认这是作业。在我无法在其他地方找到合适的答案之后,我只是作为最后的手段。我的任务是让我在帖子之间传递信息,而不使用会话变量或cookie中的cookie。基本上,当用户继续猜测一个隐藏的变量时,会带来所有过去的猜测。我试图建立一个字符串变量,它拥有所有,然后将其分配给后变量,但我不能得到任何东西来读取guessCounter变量我要么应该添加到我的代码行中得到一个未定义的索引错误字符串变量或即时通讯只是没有得到任何东西传递。这里是我的代码任何帮助将不胜感激,因为我已经在这一段时间了。使用post方法传递信息而不使用会话变量

<?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 to play the 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 even a 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']." is wrong. Try again(4)</font>"; 
       $passItOn = $_POST['guessCounter'].$passItOn; 
      } 
      if($_POST['playerGuess']==$wordChoices[$theRightAnswer]) { 
       $textToPlayer = "<font color = 'red'>You guessed ".$_POST['playerGuess']." and 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 this page</a> 
<h3>Guess the 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; 



?> 
+0

您允许使用Cookie吗? – Vector 2014-09-23 05:21:00

+0

也没有cookies,应该在主要问题中加上 – Nate58 2014-09-23 05:23:37

+0

为什么你有'$ passItOn = $ _GET ['guessCounter']。$ passItOn;'用于POST数据的conoditial? – 2014-09-23 05:47:12

回答

0

这是您需要做的一个最小功能示例。还有一些小错误(如历史记录中的重复条目),但我已将这些作为练习留给了您。把它作为一个起点,并从中建立起你需要的东西。

我已经添加了评论来解释发生了什么,所以希望这很清楚。

$answer = null; 
$history = []; 
$choices = ['apple', 'grape', 'banana']; 
$message = ''; 

// check if a 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 and answer are 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 to history and set previous answer to 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 the 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> 
+0

谢谢,真的为我清除了事情,你应该如何使用爆炸/内爆 – Nate58 2014-09-24 00:49:17

+0

不客气,@NateGreene。请记住接受答案,以便将来有类似问题的人会知道你的工作。 – timclutton 2014-09-24 07:16:09