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

时间:2014-09-23 05:16:56

标签: php

我会立即承认这是作业。在其他地方找不到合适的答案后,我只是作为最后的手段。我的任务是让我在帖子之间传递信息而不使用会话变量或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;



?>

1 个答案:

答案 0 :(得分: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>