Home > Software engineering >  In a php session, how can I take an input and then display a new question and take another input for
In a php session, how can I take an input and then display a new question and take another input for

Time:11-19

In php, the trivia game works like this: start by displaying the first question to the user, and after they give a non-empty response, move on to the second question, and so-forth. Provide a progress indicator to the user. For example: Question 6 of 8.

The trivia program is in a form which must submit back to itself (trivia.php). My confusion is that if I have a page with a question and a textbox, then the user submits their answer and I add it as a session variable, how do I then display the next question and textbox instead of the previous (while staying on the same page).

Here is what I have so far:

<?php
echo "<div class='questionHeader'><label>Question 1 of 6</label></div>";
echo "<br>";
echo "<div class='question'>" . $ques[0] . "</div>";
echo "<br>";
echo "Answer: ";
echo "<input type='text' id='answerOneSub' name='answerOneSub'>";
echo "<button type='submit' value='submit' name='submit'>Submit!</button>";

if (isset($_POST['submit'])) {

    $_SESSION['answerOneSub'] = $_POST['answerOneSub'];
    echo "<br>" . $_SESSION['answerOneSub'];
}
?>

When the user has completed all the questions, it must display a table of the results on the same page.

CodePudding user response:

You could have another session that stores the answers as an array, and add to it after each successful post

Something like this could work:

 <?php
    $totalQuestions = count($ques);
    $_SESSION['answers'] = $_SESSION['answers'] ?? [];
    // Get current question, default to 1
    $currentQuestion = count($_SESSION['answers']) == $totalQuestions ? 
        $totalQuestions : 
        $_SESSION['answers']   1;
?>

<div class='questionHeader'>
    <label>Question <?php echo $currentQuestion ?> of <?php echo $totalQuestions ?></label>
</div>

<br>

<div class='question'>
    <?php echo $ques[$currentQuestion-1] ?>
</div>

Answer: <input type='text' id='answerOneSub' name='answerOneSub'>
<button type='submit' value='submit' name='submit'>Submit!</button>
 
<?php
    if (isset($_POST['submit'])) {
        $_SESSION['answers'][] = $_POST['answerOneSub'];
        echo "<br>" . $_SESSION['answerOneSub'];
    }
?>

CodePudding user response:

You can use query param like this:

<?php
$page = 1;
if (isset($_GET["page"])) {
    $page = (int)$_GET["page"];
}

$url = strtok($_SERVER["REQUEST_URI"], '?');
echo "<form method='post' action='" . $url . "?page=" . ($page   1) . "'>";
echo "<div class='questionHeader'><label>Question [$page] of 6</label></div>";
echo "<br>";
echo "<div class='question'>" . $ques[$page - 1] . "</div>";
echo "<br>";
echo "Answer: ";
echo "<input type='text' id='answerOneSub' name='answerOneSub'>";
echo "<button type='submit' value='submit' name='submit'>Submit!</button>";
echo "</form>";

if (isset($_POST['submit'])) {
    $_SESSION['answerOneSub'] = $_POST['answerOneSub'];
    echo "<br>" . $_SESSION['answerOneSub'];
}
  • Related