skip to Main Content

}

put returns between paragraphs
for linebreak add 2 spaces at end
italic or bold
indent code by 4 spaces
backtick escapes like _so_
quote by placing > at start of line
to make links (use https whenever possible)

2

Answers


  1. 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'];
    }
    
    Login or Signup to reply.
  2. 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'];
        }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search