php $response在用户输入答案后显示两次

tf7tbtn2  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(122)

我已经试着修复我的问题好几天了,我希望有人能给予我一个关于如何修复它的建议。我可能忽略了代码,但这里是完整的内容。

<?PHP
    include 'functions/generate_word.php';

    $num_words_generated = isset($_SESSION['num_words_generated']) ? $_SESSION['num_words_generated'] : 0;
    $random_word_jumbled = str_shuffle($currentWord);

    if ($num_words_generated <= 10)
    {
        $_SESSION['num_words_generated'] =+ $num_words_generated;
        $item_num = $_SESSION['num_words_generated'];

这是出现了两次的$响应。

$response = "<div id = 'output-container'><p>Question #" . $item_num . "<br>Unscramble the word: <strong>" . $random_word_jumbled . "</strong></p></div>";
    }
    else
    {
        $response = "<div id = 'output-container'><p>You have finished the quiz!<br>Go back to <a class = 'button' href = 'home.php'>home page.</a></p></div>";
    }
?>

<div id = "chat-container">
    <?PHP
        // Initialize an empty session variable for storing the chat history
        if (!isset($_SESSION['chat_history']))
        {
            $_SESSION['chat_history'] = array();
        }

        array_push($_SESSION['chat_history'], $response);

        // Loop through the chat history and display each message
        foreach ($_SESSION['chat_history'] as $message)
        {
            if (strpos($message, "input-container") !== false)
            {
                // Display user's message with profile photo on the right side
                echo "<div><img src = 'css/child.png' class = 'pfp-right'>" . $message . "</div>";
            }
            else
            {
                // Display AI's response with profile photo on the left side
                echo "<div><img src = 'css/koala.png' class = 'pfp-left'>" . $message . "</div>";
            }
        }
    ?>
</div>

<form method = "POST">
    <div id = "input-container">
        <?PHP
            // Disable the input field when the counter has reached its limit
            if ($num_words_generated >= 10)
            {
                echo '<input type = "text" name = "user_input" autocomplete = "off" autofocus disabled>';
                echo '<button type = "submit" name = "submit" disabled>Send</button>';
            }
            else
            {
                echo '<input type = "text" name = "user_input" autocomplete = "off" autofocus>';
                echo '<button type = "submit" name = "submit">Send</button>';
            }
        ?>
    </div>
</form>

<?PHP
    if (isset($_POST['submit']) && $_SERVER["REQUEST_METHOD"] == "POST")
    {
        $user_input = $_POST['user_input'];

        // Add the user's input to the chat history
        array_push($_SESSION['chat_history'], "<div id = 'input-container'><p>" . $user_input . "</p></div>");

        // Process the user's input and generate a response
        if (strtolower($user_input) == strtolower($lastWord))
        {
            $response = "<div id = 'output-container'><p>Correct!</p></div>";

            if (!isset($_SESSION['score']))
            {
                $_SESSION['score'] = 10;
            }
            else
            {
                $_SESSION['score'] += 10;
            }

            $total_score = $_SESSION['score'];

            $response .= "<div id = 'output-container'><p>Score: " . $total_score . "</p></div>"; 
        }
        else
        {
            $response = "<div id = 'output-container'><p>Incorrect. The correct answer is: " . $lastWord . "</p></div>";
        }

        // Add the AI's response to the chat history
        array_push($_SESSION['chat_history'], $response);

        // Redirect to the current page to clear the input field
        header("Location: ".$_SERVER['REQUEST_URI']);
        ob_end_flush();
    }
?>

下面是输出以供参考:output reference of the bug
我试着省略了一些行,并修改了一些,仍然输出与上面的图像相同。
PS.对于之前看到我问题的人,我很抱歉我没有发布足够的细节。

sycxhyv7

sycxhyv71#

你的问题与这些线的上下部分有关:

array_push($_SESSION['chat_history'], $response);

        // Loop through the chat history and display each message
        foreach ($_SESSION['chat_history'] as $message)

这些行上面的部分每次都会执行。当他们发布响应/答案时,您希望它首先处理该响应/答案,然后生成一个新的随机加扰单词,但它没有。它每次都首先执行随机生成器,然后将其添加到chat_history数组中,然后再处理响应。
您可能希望将$_POST处理移到脚本的顶部,并使用它来决定是先处理POST,还是先生成一个新的随机数。
换句话说,当用户单击submit时,整个脚本将从头开始执行,而不是“从中断的地方继续执行”(在脚本的中间)。

相关问题