使用不带会话变量的post方法传递信息


Passing information using post method without session variables

我会立即承认这是家庭作业。在其他地方找不到合适的答案后,我只能在这里作为最后的手段。我的任务是让我在不使用php中的会话变量或cookie的情况下在帖子之间传递信息。从本质上讲,当用户继续猜测一个隐藏的变量时,会将过去的所有猜测延续到那个点。我正试图构建一个包含所有这些变量的字符串变量,然后将其分配给post变量,但我无法从guessCounter变量中读取任何内容。我要么在应该添加到字符串变量的代码行中出现未定义的索引错误,要么根本没有得到任何传递。这是我的代码,任何帮助都将不胜感激,因为我已经在这方面工作了一段时间。

  <?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;

?>

这是一个关于您需要做什么的最小功能示例。仍然存在一些小错误(如历史记录中的重复条目),但我将这些作为练习留给您。把这当作一个起点,并从中建立你需要的东西。

我添加了一些评论来解释发生了什么,所以希望你能清楚。

$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="answerswer" 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>