php mcq – html code for online quiz program

php mcq : online quiz project in php and mysql, php quiz code, php mysql multiple choice quiz source code and php coding test questions.

php mcq – php coding test questions

Source Code of Online Quiz in PHP : Our 10+ PHP Programming MCQ (Multiple Choice Questions and Answers) focus on all areas of PHP Programming covering 5+ topics.

Free Live Chat for Any Issue

Here, i will create a web-based application(php mcq) for php mysql multiple choice quiz source code.

For making the php quiz code application, i needed to develop the following all the files;

  1. database_connection.php
  2. index.php
  3. retrievemcqTest.php
  4. retrivesolutions.php
  5. saveallmcq.php

PHP MCQ Questions : This section focuses on “Basics” of PHP.

php mysql multiple choice quiz source code

database_connection.php
This PHP file source contains all code about database_connection.php.

<?php
$host="localhost";
$user="root";
$pass="";
$db="pakainfo_v1";
$con = new mysqli($host, $user, $pass, $db)
or die ('Could not connect to the database server' . mysqli_connect_error());
?>

Don’t Miss : php mysql multiple choice quiz source code

index.php
This main file data contains all source code about main root file index.php.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quiz Application - online quiz project in php and mysql</title>
</head>
<body>
<div style="text-align:center;">
<p style=" font-size: 400%; ">MCQs Application - www.pakainfo.com</p>
</div>
<div style="text-align:center;">
Enter your name:   <input type="text" id="membername">    
<button type="button" onclick="takemcqTest()" id="btnTake"> Take mcqTest </button>
</div>
<div style="text-align:center;" id="mcqTestsection"></div>
<div style="text-align:center;" id="solutionsection"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
getSolutions();
var memberName;
function takemcqTest(){
var name = $("#membername").val();
$("#mcqTestsection").replaceWith('<div style="text-align:center;" id="mcqTestsection"></div>');
if (name){
$("#solutionsection").hide();
	memberName = name;
	$.ajax({
	url: 'getmcqTest.php',
	type: 'POST',
	data: {value: 'take', name: memberName },
	success: function (solution) {
		$("#mcqTestsection").replaceWith(solution);
	}
	});
}
else {
getSolutions();
$("#solutionsection").show();
}
}
function getSolutions(){
	$.ajax({
		url: 'getsolutions.php',
		type: 'POST',
		data: {value: 'solution'},
		success: function (solution) {
			$("#solutionsection").replaceWith(solution);
		}
	});
}
</script>
</body>
</html>

retrievemcqTest.php
This PHP file data contains all code about get a allthe the mcqTest data.

<?php
require('database_connection.php');
$no = 1;
if (isset($_POST['value'])){
$take = $_POST['value'];
$membername = $_POST['name'];
if (!empty($take) || !empty($membername)){
echo '<div style="text-align:center;" id="mcqTestsection">';
echo '<form action="saveallmcq.php" method="POST">';
echo '<input type="hidden" name="nameofmember" value="'.$membername.'">';
// get mcq McqQues and their options
$sqlMCQs = "SELECT * FROM questions_master";
$solutionMCQs = mysqli_query($con, $sqlMCQs);
while ($rowMCQs = mysqli_fetch_assoc($solutionMCQs)){
$id = $rowMCQs['id'];
$question = $rowMCQs['question'];
$selection1 = $rowMCQs['op1'];
$selection2 = $rowMCQs['op2'];
$selection3 = $rowMCQs['op3'];
$answer = $rowMCQs['answer'];
echo '<br><div style="margin: auto; border-radius: 25px; border: 2px solid black; padding: 20px; width: 800px">';
echo '<h3>Question #'.$no.'</h3><br>';
echo '<p style=" font-size: 150%; ">'.$question.'</p><br>';
echo '<h3>Options</h3><br>';
echo '<input type="radio" name="'.$id.'" value="'.$selection1.'" id="'.$selection1.'" checked>'.$selection1.'    ';
echo '<input type="radio" name="'.$id.'" value="'.$selection2.'" id="'.$selection2.'">'.$selection2.'    ';
echo '<input type="radio" name="'.$id.'" value="'.$selection3.'" id="'.$selection3.'">'.$selection3.'    ';
echo '</div>';
$no++;
}
echo '<br><button style="font-size: 120%;" name="submit" type="submit" id="btnSubmit"> Submit mcqTest </button>';
echo '</form>';
echo '</div>';
}
}

retrivesolutions.php
This data file contains all source code about get the all solutions.

<?php
require('database_connection.php');
$no = 1;
if (isset($_POST['value'])){
echo '<div style="text-align:center;" id="solutionsection"><p style="color: red;">Enter your name and click Take mcqTest button!</p><br>';
echo '<table style="width:30%; border-collapse: collapse; margin: auto;"><thead><tr><th style="border: 1px solid black; border-collapse: collapse; ">#</th><th style="border: 1px solid black; border-collapse: collapse; ">Name</th><th style="border: 1px solid black; border-collapse: collapse; ">Score</th></tr></thead><tbody>';
$sqlScore = "select * from member";
$solutionScore = mysqli_query($con, $sqlScore);
while ($rowScore = mysqli_fetch_assoc($solutionScore)){
$name = $rowScore['name'];
$score = $rowScore['score'];
echo '<tr><td style="border: 1px solid black; border-collapse: collapse; ">'.$no.'</td><td style="border: 1px solid black; border-collapse: collapse; ">'.$name.'</td><td style="border: 1px solid black; border-collapse: collapse; ">'.$score.'</td></tr>';
$no++;
}
echo '</tbody></table>';
echo '</div>';
}

saveallmcq.php
This php file data contains all the source code about submitting or save the quiz.

<?php
require('database_connection.php');
if (isset($_POST['submit'])){
$nameofmember = $_POST['nameofmember'];
$totalMcqQues = 0;
$correctAnswers = 0;
foreach($_POST as $key => $value) {
if($key == 'nameofmember'){
}else if ($key == 'submit') {
}
else{
$tempAnswer = $_POST[$key];
// count total McqQues and correct answers
$sqlAnswer = "select  count(*) count from questions_master where id = '$key' and answer = '$tempAnswer'";
$solutionAnswer = mysqli_query($con, $sqlAnswer);
$rowAnswer = mysqli_fetch_assoc($solutionAnswer);
$numAnswer = $rowAnswer['count'];
if ($numAnswer < 1){
// wrong answer
}
else {
// correct answer
$correctAnswers++;
}
$totalMcqQues++;
}  
}
// Store score in db
$sqlSubmit = "insert into member (name, score) values ('$nameofmember', '$correctAnswers/$totalMcqQues')";
if (mysqli_query($con, $sqlSubmit)){
header("Location: index.php?status=succes");
} else {
header("Location: index.php?status=error");
}
}

I hope you get an idea about php quiz code – how to create quiz in php and mysql?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.