0

I want to display right or wrong when someone selects a radio button.

Don't know how to do it. This is what I've done up until now. I've made the database to store questions, options and correct answer. I've done pagination to show ONE Question on one page and then user can go to the next question using the NEXT button.

<?php
if (isset($_GET['pageno'])){
    $pageno = $_GET['pageno'];
} else {
    $pageno = 1;
}

$no_of_records_per_page = 1;
$offset = ($pageno - 1) * $no_of_records_per_page;
$conn = mysqli_connect("localhost", "root", "", "quizdb");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
}

$total_pages_sql = "SELECT COUNT(*) FROM entry1";
$result = mysqli_query($conn, $total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);

$sql = "SELECT * FROM entry1 LIMIT $offset, $no_of_records_per_page";
$res_data = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_array($res_data)) {
    include('server1.php');
    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'quizdb';

    $con = mysqli_connect($host, $user, $pass, $db);
    $qno = $row["qno"];
    $question = $row["question"];
    $optiona = $row["optiona"];
    $optionb = $row["optionb"];
    $optionc = $row["optionc"];
    $optiond = $row["optiond"];
    $ans = $row["ans"];

    echo '<br>Q ' . $qno . '. ';
    echo '' . $question . '<br>';
    echo '<input type="radio" name="options" value="optiona"></button>' . $optiona . '<br>';
    echo '<input type="radio" name="options" value="optionb"></button>' . $optionb . '<br>';
    echo '<input type="radio" name="options" value="optionc"></button>' . $optionc . '<br>';
    echo '<input type="radio" name="options" value="optiond"></button>' . $optiond . '<br>';
    echo '<form action="practice.php" method="post">
    <input type="submit" name="submitans" value="Check Answer"></form>';
           if (isset($_POST['submitans'])) {
               echo '' . $ans . '<br>';
           }
}
mysqli_close($conn);
?>
<ul class="pagination">

    <li><a href="?pageno=1">First</a></li>

    <li class="<?php if ($pageno <= 1) {
        echo 'disabled';
    } ?>">

        <a href="<?php if ($pageno <= 1) {
            echo '#';
        } else {
            echo "?pageno=" . ($pageno - 1);
        } ?>">Prev</a></li>

    <li class="<?php if ($pageno >= $total_pages) {
        echo 'disabled';
    } ?>">

        <a href="<?php if ($pageno >= $total_pages) {
            echo '#';
        } 

    else {
        echo "?pageno=" . ($pageno + 1);
    } ?>">Next</a>
   </li>
   <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
</ul>
</body>
</html>

Expect : To display Right or Wrong Answer when someone selects an option.

  • If you want to display `Right` or `Wrong` immediately when someone selects (clicks) on an option, just PHP (being server-side language) is not your answer. You'd have to combine JavaScript's AJAX calls with PHP. – Kacper Turowski Jul 11 '19 at 13:37
  • See: [What's the difference between client-side and server-side programming](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – CD001 Jul 11 '19 at 14:16

0 Answers0