0

I'm a beginner programmer and I'm busy setting up a PHP online quizzer site and I'm running into a bit of a problem with my choice selection.

I need my radio button to stay selected when going to the next page.

Here's my code for the page:

Each time I click next and go back to the previous page my selection is erased. How do I keep it saved?


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf=8">
        <title>PHP Quizzer</title>
    </head>

    <body>
        <header>
            <div class = "container">
                <h1>PHP Quizzer</h1>
            </div>
        </header>

        <main>
            <div class="container">
                <div class="current"> Question  <?php echo $question['question_number']; ?> of <?php echo $total; ?> </div>
                <p class="question">
                    <?php echo $question['text']; ?>
                </p>

                <form method="post" action="process.php">
                    <ul class="choices">

                        </select>

                        <?php while($row =$choices-> fetch_assoc()) : ?>
                        <li><input name="choice" type="radio" value="<?php echo $row['id']; ?>" /><?php echo $row['text'] ; ?></li>
                        <?php endwhile; ?>
                    </ul>

                    <input type="submit" value="Submit" />
                    <input type="hidden" value="<?php echo $number;?>" name="number"/>
                    <!--<a href="questions.php?n=1" class = "previous">Previous</a>-->
                    <!--<a href="questions.php?n=" name = "next">Next</a>-->

                    <?php
                        $next_button='';
                        $previous_button='';
                        $enabled='TRUE';
                        if ($number<$total)
                        {
                            $number++;
                            $next_button="<a href='questions.php?n=$number name='next'>Next</a>";
                            echo($next_button);
                        }

                        if ($number>2)
                        {
                            $number--;
                            echo "<a href='questions.php?n=" . ($number-1) . "' class='button'>Previous</a></li>";
                            #$previous_button="<a href='questions.php?n= name='previous'>Previous</a>";
                            echo($previous_button);
                        }
                    ?>
                </form>
            </div>
        </main>

        </header>

    </body>
</html>

<?php

    #if(count($id_list) < $num) {die("I need more questions in the database.");}
?>

Here's what I got in where my radio buttons are:

<?php while($row =$choices-> fetch_assoc()) : ?>
            <li><input name="choice" type="radio" value="<?php echo $row['id']; ?>" /><?php echo $row['text'] ; ?></li>
            <?php endwhile; ?>
        </ul>
        <input type="submit" value="Submit" />
        <input type="hidden" value="<?php echo $number;?>" name="number"/>

I need my selection to save each time I go to the next page.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Apxllo
  • 1
  • Possible duplicate of [Keep values selected after form submission](https://stackoverflow.com/questions/2246227/keep-values-selected-after-form-submission) – Phil Jun 26 '19 at 13:01
  • 2
    Possible duplicate of [checkbox checked with php form post?](https://stackoverflow.com/questions/2792375/checkbox-checked-with-php-form-post) – ArtisticPhoenix Jun 26 '19 at 13:05
  • The sample HTML is not well formed. E.g `` does not have a corresponding start tag. The same for one of the `` tags. You can use the [W3C markup validation service](http://validator.w3.org/) to check your HTML (after PHP processing). – Peter Mortensen Jul 08 '19 at 01:03

0 Answers0