2014-09-23 16 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