Peter Ypma Peter Ypma - 1 year ago 56
PHP Question

Continue to next page when filling in right value

I'm making a quiz for my students. I would like to make it in such a way that you can only see question 2 after you solved question 1 (and so on).

My current idea is to make a web page for every question with a form on it:

<FORM action="test.php" method="post">
<I>12 is the right answer:</I>
<INPUT TYPE="text" NAME="name" SIZE="20" MAXLENGTH="30"><BR>
<INPUT TYPE="submit" VALUE="send">

And afterwards, I try to redirect from test.php to next.php whenever the answer is 12 and to current.php when the answer is not 12. Though, I am not able to make this work. Can anyone help me?

Answer Source

Have not written php code for a while now but will try to guide you.

No, you should not create a web page for each question. That is not a scalable approach. Imagine if you have to store 1000 questions over time,period.

Instead use dynamic page loading concept in php. Here what you should be doing:

1.Create a table in whichever database you are using with fields like

  1. Create a php page like quiz.php which reads the question based on questionId stored in the session variable.

Lets say you show the first question in front page, displad the options and showed a submit button.

When the user clicks submit button what should happen is that the same quiz.php page will get called courtsy <form action="quiz.php"> with the user selected answer.

You can then check for the correct answer in php file since you have the correct answer stored in a variable which stores the database fetched result of first question, and if that is correct answer you can update the session with id of next question (increment by 1 or any other mechanism) and query the database table for that question id .

Your html should be written in such a way away which reads the current value stored in $row variable, which stores the result of the query w.r.t to questionId stored in session.

little bit of the code :

quiz.php file:

if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db($dbname, $con);

// Check user answer for previous question
if (isset($_POST['submit'])) {   
    $previous_question_id = (int) $_POST['question_id']; 

    // Query database
    $getQuestion = "SELECT * from questions_table where id =    
    $resultOfQuestion = mysqli_query($conn, $getQuestion);

    $correct = $row_get_question['correctAnswer'];
    $selected_radio = $_POST['answer'];

    if ($selected_radio == $correct)
        echo "THAT ANSWER IS CORRECT";
        $_SESSION['question_id'] = $previous_question_id +1;

        $getQuestion = "SELECT * from questions_table where   
        $resultOfQuestion = mysqli_query($conn, $getQuestion);

        echo "THAT ANSWER IS WRONG!";


if(!isset($_SESSION['question_id'])) {
$getQuestion =  mysql_query("SELECT * FROM questions WHERE    
$resultOfQuestion       = mysqli_query($conn, $getQuestion);

echo "<form  action='quiz.php' method='post'>";
while($row = mysql_fetch_array($resultOfQuestion)){

    echo " <b>" . $row['question'] . "</b>";

    echo "<input type='radio' value='' name='answer' checked />"; 
    echo  $row['option1']; echo "<br />";
    echo "<input type='radio' value='' name='answer' />"; 
    echo  $row['option2'];echo "<br />";
    echo "<input type='radio' value='' name='answer' />";
    echo  $row['option3'];echo "<br />";
    echo "<input type='radio' value='' name='answer' />"; 
    echo  $row['option4'];echo "<br />";

    echo "<input type='submit' name='next' value='next' />";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download