How to create multiple choice questions and answers in PHP
In this article, you will learn how to create multiple choice quiz questions and answers using the PHP programming language.
Today, online quiz have a lot of advantages. It is considered an easy solution to conduct a quiz, analyse the results, and generate a report. It is also advantageous for the security and confidentiality of quiz questions and answers.
Here, we have created a main PHP file, 'index.php', that we will call in the browser, and have written multiple choice questions and answers using HTML. When the user submits the quiz form, it will redirect to the 'quiz.php' page.
index.php
<html>
<head>
<title>PHP Multiple Choice Questions and Answers</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Multiple Choice Questions Answers</h1>
<p>Please fill the details and answers the all questions-</p>
<form action="score.php" method="post">
<div class="form-group">
<strong>Name*:</strong><br/>
<input type="text" name="name" value="" required/>
</div>
<div class="form-group">
<strong>Age*:</strong><br/>
<input type="text" name="age" value="" required/>
</div>
<div class="form-group">
<strong>Phone*:</strong><br/>
<input type="text" name="phone" value="" required/>
</div>
<h3>Ques1 : Who is the father of PHP? </h3>
<div class="form-group">
<ol>
<li>
<input type="radio" name="q1" value="1" />Rasmus Lerdorf
</li>
<li>
<input type="radio" name="q1" value="2" />Larry Wall
</li>
<li>
<input type="radio" name="q1" value="3" />Zeev Suraski
</li>
</ol>
</div>
<br/>
<div class="form-group">
<h3>Ques2 : Which of the functions is used to sort an array in descending order?</h3>
<ol>
<li>
<input type="radio" name="q2" value="1" />sort()
</li>
<li>
<input type="radio" name="q2" value="2" />asort()
</li>
<li>
<input type="radio" name="q2" value="3" />rsort()
</li>
</ol>
</div>
<br/>
<div class="form-group">
<h3>Ques3 : Which version of PHP introduced the instanceof keyword?</h3>
<ol>
<li>
<input type="radio" name="q3" value="1" />PHP 4
</li>
<li>
<input type="radio" name="q3" value="2" />PHP 5
</li>
<li>
<input type="radio" name="q3" value="3" />PHP 6
</li>
</ol>
</div>
<div class="form-group">
<input type="submit" value="Submit" name="submit" class="btn btn-primary"/>
</div>
</form>
</div>
</body>
</html>
score.php
Next, we have created a PHP page 'score.php' that contains input and multiple choice answers, validation code, and calculates the score level of the user.
<?php
if($_POST['submit']) {
$name = $_POST['name'];
$age = $_POST['age'];
$phone = $_POST['phone'];
if($name == '' || $age == '' || $phone == '') {
echo '<h2>Please fill all * mandatory fields.</h2>';
}
if($q1=='' || $q2 =='' || $q3 =='')
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
if($q1=='' || $q2 =='' || $q3 =='') {
echo '<h2>Please answer all questions.</h2>';
}
else {
$score = 0;
if($q1 == 1) { // 1st option is correct
$score++;
}
if($q2 == 3) { // 3rd option is correct
$score++;
}
if($q3 == 2) { // 2nd option is correct
$score++;
}
$score = $score / 3 *100;
if($score < 50)
{
echo '<h2>You need to score at least 50% to pass the exam.</h2>';
}
else {
echo '<h2>You have passed the exam and scored '.$score.'%.</h2>';
}
}
}
?>
If the user has filled in all the fields and answered all the questions correctly, it returns the following output.
You have passed the exam and scored 100%.
Related Articles
PHP sanitize input for MySQLPHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Display PDF file from Database
How to read CSV file in PHP and store in MySQL
Generating word documents with PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Simple PHP email form
Password reset system in PHP
HTTP authentication with PHP
PHP file cache library
PHP get current directory url
How to prevent CSRF attack in PHP
Forgot Password Script PHP mysqli database
PHP Contact Form with Google reCAPTCHA
HTML Form Validation in PHP