Jump to content

PHP Quiz Validation


Irate
Go to solution Solved by Jessica,

Recommended Posts

Hey there,

 

so, basically, I have been reading a few tutorials about making a quiz with PHP, so I decided to write my own quiz, but I am stuck at the answer validation action because I do not know how to properly do so (I am a bit new to PHP, too), so I decided to ask about this.

I haven't got a mysql db yet, that noted, and I want the whole quiz to be displayed within an iframe html tag on another site of mine which does not support PHP hosting itself.

 

Code:

 

<?php // quiz.php

header("Content-Type: text/html; charset=utf-8");

?>

<!-- exclude basic html skeleton -->

<form action="validate.php" method="post">

 

Question 1:

Question Text

<input type="radio" value="0">Answer 1 (false, as value="0" indicates)<br>

<!-- repeat two more times -->

<input type="radio" value="1">Answer 4 (this is my correct answer)<br>

 

<!-- repeat questions until #35 -->

<!-- end quiz.php, close html tags -->

 

<?php // validate.php

$res = 0;

foreach($_POST as $score){ // get all answers from post array

$res = $res + $score;

}

switch($res){ // do switch statement to avoid endless elseif statements because we have many answers

case 35:

echo "To submit your score, press the button below.<br><form action='http://forum.forumotion.com/posting.forum?' method='post'>

<textarea style='display: none;'>Scored ".$res." out of 35 possible points.</textarea>

<input type='submit'></form>"; exit; break;

case 34: // repeat until reached at 0

echo "..."; exit; break;

}

?>

 

The forum URL specified within validate.php is fictional, by the way, but my forum host uses /posting.forum? for its post action.

Thing is, I don't want to specify the value of the answers with HTML because about everyone could inspect the HTML code of the iframe and get every answer correct...

I want to prevent that, of course.

Anyone help?

Edited by Irate
Link to comment
Share on other sites

An ini-style text file makes a reasonable alternative to a database for small, simple application of this nature and doesn't require lengthy processing. Here's a simple example

 

quiz1.txt

[1]
qtext="This is question 1?"
A="Q1 Answer 1"
B="Q1 Answer 2"
C="Q1 Answer 3"
D="Q1 Answer 4"
correct=C

[2]
qtext="This is question 2?"
A="Q2 Answer 1"
B="Q2 Answer 2"
C="Q2 Answer 3"
D="Q2 Answer 4"
correct=A

[3]
qtext="This is question 3?"
A="Q3 Answer 1"
B="Q3 Answer 2"
C="Q3 Answer 3"
D="Q3 Answer 4"
correct=B

quizform.php

<html>
<head>
<meta name="generator" content="PhpED Version 8.1 (Build 8115)">
<title>My Quiz Form</title>
<meta name="author" content="Barand">
</head>
<body>

<h1>Quiz</h1>
<form action='quizresult.php' method='post'>
<?php
    // load the quiz
    $quiz = parse_ini_file('quiz1.txt', true);
    
    // display the quiz
    foreach ($quiz as $qno => $qdata) {
        foreach ($qdata as $k => $v) {
            switch ($k) {
                case 'correct':
                    continue;
                case 'qtext':
                    echo "<h3>$v</h3>";
                    // dummy answer in case no answer given
                    echo "<input type='hidden' name='answer[$qno]' value=''>";
                    break;
                default:
                    echo "<input type='radio' name='answer[$qno]' value='$k'> $v<br>";
                    break;
            }
        }
    }
    echo "<br><input type='submit' name='btnSubmit' value='Submit'>";
?>
</form>
</body>
</html>

quizresult.php

<?php
    if (!isset($_POST['answer'])) {
        header("location: quizform.php");
        exit;
    }
    $quiz = parse_ini_file('quiz1.txt', true);
    // check answers
    $qtot = count($quiz);
    $totCorrect = 0;
    $results = "<tr><th>Question</th><th>Your answer</th><th>Correct answer</th></tr>";
    foreach ($_POST['answer'] as $qno => $ans) {
        $results .= "<tr><td>{$quiz[$qno]['qtext']}</td><td>{$quiz[$qno][$ans]}</td>";
        if ($ans == $quiz[$qno]['correct']) {
            ++$totCorrect;
            $results .= "<td>YES</td></tr>";
        } else {
            $results .= "<td>{$quiz[$qno][$quiz[$qno]['correct']]}</td></tr>";
        }
    }
    
?>
<html>
<head>
<meta name="generator" content="PhpED Version 8.1 (Build 8115)">
<title>My Quiz Results</title>
<meta name="author" content="Barand">
</head>
<body>
<h1>Quiz results</h1>
    <table border='1'>
        <?php echo $results ?>
    </table>
    <?php
        printf ("You scored %d out of %d (%0.2f%%)", $totCorrect, $qtot, $totCorrect*100/$qtot);
    ?>
    
</body>
</html>
Link to comment
Share on other sites

Does depend on how much you want to store. Personally when I think of an application like a quiz, I immediately think of all the functionality you could include and assume you'll want to :)

 

You could definitely use just a flat file. I'd use something like YAML or XML then.

Link to comment
Share on other sites

Seeing as I want to include 35 Questions with each 2 to 4 radio buttons and 4 essay questions, I think SQL would my weapon of choice once I get around to properly administering my own database, then I think I will be ready to move onto an external server host and will post a new topic about this.

 

Thank you for your input.

Link to comment
Share on other sites

You could definitely use just a flat file.

YAML, XML, INI. Did everybody forget there are other DB vendors then MySQL? Like SQLite?! A lot easier to switch later on to for example MySQL too. Also if for some reason the application would become more popular supports indexing and other things we have come to expect of a database.

Edited by ignace
Link to comment
Share on other sites

YAML, XML, INI. Did everybody forget there are other DB vendors then MySQL? Like SQLite?! A lot easier to switch later on to for example MySQL too. Also if for some reason the application would become more popular supports indexing and other things we have come to expect of a database.

.... Yours was the first post on this thread to mention MySQL

Link to comment
Share on other sites

I mentioned MySQL to infer this was more work to setup which is why I suppose everyone opted for XML, YAML, or INI. And I simply wanted to remind everyone there is also SQLite (IMO a far better choice then XML, YAML, or INI).

Edited by ignace
Link to comment
Share on other sites

I'll give SQLite a try if you say it's easier to set up.

 

I have set-up books about PHP and SQL (I have IIS set up correctly already) but I can't find the rograms because the books are German and my PC language is English >.<

Link to comment
Share on other sites

Since you speak german (well a dialect of sorts) and also understand english (since your pc is set to it) I don't think it will be too hard to translate between those two languages. Otherwise if you happen to have Win7 Ultimate edition you can just select a different system language (or using the Anytime Upgrade you can upgrade to ultimate).

Edited by ignace
Link to comment
Share on other sites

  • 4 weeks later...

 

An ini-style text file makes a reasonable alternative to a database for small, simple application of this nature and doesn't require lengthy processing. Here's a simple example

 

quiz1.txt

[1]
qtext="This is question 1?"
A="Q1 Answer 1"
B="Q1 Answer 2"
C="Q1 Answer 3"
D="Q1 Answer 4"
correct=C

[2]
qtext="This is question 2?"
A="Q2 Answer 1"
B="Q2 Answer 2"
C="Q2 Answer 3"
D="Q2 Answer 4"
correct=A

[3]
qtext="This is question 3?"
A="Q3 Answer 1"
B="Q3 Answer 2"
C="Q3 Answer 3"
D="Q3 Answer 4"
correct=B

quizform.php

<html>
<head>
<meta name="generator" content="PhpED Version 8.1 (Build 8115)">
<title>My Quiz Form</title>
<meta name="author" content="Barand">
</head>
<body>

<h1>Quiz</h1>
<form action='quizresult.php' method='post'>
<?php
    // load the quiz
    $quiz = parse_ini_file('quiz1.txt', true);
    
    // display the quiz
    foreach ($quiz as $qno => $qdata) {
        foreach ($qdata as $k => $v) {
            switch ($k) {
                case 'correct':
                    continue;
                case 'qtext':
                    echo "<h3>$v</h3>";
                    // dummy answer in case no answer given
                    echo "<input type='hidden' name='answer[$qno]' value=''>";
                    break;
                default:
                    echo "<input type='radio' name='answer[$qno]' value='$k'> $v<br>";
                    break;
            }
        }
    }
    echo "<br><input type='submit' name='btnSubmit' value='Submit'>";
?>
</form>
</body>
</html>

quizresult.php

<?php
    if (!isset($_POST['answer'])) {
        header("location: quizform.php");
        exit;
    }
    $quiz = parse_ini_file('quiz1.txt', true);
    // check answers
    $qtot = count($quiz);
    $totCorrect = 0;
    $results = "<tr><th>Question</th><th>Your answer</th><th>Correct answer</th></tr>";
    foreach ($_POST['answer'] as $qno => $ans) {
        $results .= "<tr><td>{$quiz[$qno]['qtext']}</td><td>{$quiz[$qno][$ans]}</td>";
        if ($ans == $quiz[$qno]['correct']) {
            ++$totCorrect;
            $results .= "<td>YES</td></tr>";
        } else {
            $results .= "<td>{$quiz[$qno][$quiz[$qno]['correct']]}</td></tr>";
        }
    }
    
?>
<html>
<head>
<meta name="generator" content="PhpED Version 8.1 (Build 8115)">
<title>My Quiz Results</title>
<meta name="author" content="Barand">
</head>
<body>
<h1>Quiz results</h1>
    <table border='1'>
        <?php echo $results ?>
    </table>
    <?php
        printf ("You scored %d out of %d (%0.2f%%)", $totCorrect, $qtot, $totCorrect*100/$qtot);
    ?>
    
</body>
</html>

 

Hi Barand, 

 

I am so much impressed with your quiz solution - I am also trying to execute a PHP quiz test on my website.

How can I hold two parameters with each answer and fetch them to calculate result and display it. 

 

E.g., Just see it-

 

Q 1. Which is your favorite color?

 

A. Red (P=p1, Q=3)

B. White (P=p3, Q=4)

C. Green (P=p2, Q= -1)

D. Blue (P=p4, Q= -2)

 

 

Is it possible to hold two parameters with each answer and retrieve the chosen answer. i.e., my user chosen "B" answer of the above question then - How can I fetch associated parameters with white (B - user chosen answer) - It's associated parameters are "(P=p3, Q=4)" which are not visible to the user.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.