PHP Quiz with Countdown Timer

hey Tochnoz , In this tutorial we are going to create PHP Quiz with Countdown timer. In previous tutorial, We have learn how to create simple php quiz and its fundamentals. As we promised in the previous tutorial we come up with the advancement of quiz. Additionally we have provided an option for adding questions and answer into the quiz.

Creating database

A database need to be create to store the information about questions, answers and user’s submitted answers.

So lets create the database by using following queries. The following SQL  creates quiz table which consist of  id, questions, options ,answers and timer details.

Dumping data for table ‘quiz’

Now lets insert records into the table by using following queries

First, to make it convenient, we will create a new PHP file for database configuration named dbconfig.php  that holds all configured parameters:

Code of PHP Quiz with Countdown timer

Now its time to code the Quiz web application, In this we have use bit JavaScript code to modifying client data object module.

  • b.style.visibility = 'hidden';  is use to hide the start quiz button after clicking.
  • x.style.display = "block";  is use to render as a block-level element.
  • window.onload = function() { document.getElementById('myDIV').style.display = 'none'; };  is use to hide the question fetch from the database before user click to the start quiz button.
  • startTimer() this function invokes when user click the start button and it is use to display the live countdown.

The preview of above code is shown in following image:

PHP Quiz with Countdown Timer

Add Questions and timer

By following code  add_quiz.php user can able to add the question and answer with its choices into the database.

The quiz taker can set time limit for the quiz. For which separate field are provided so that we can concatenate it with  colon purposely so that we can direct fetch and allocate it into JavaScript variable.

The preview of above code is shown in following image:

PHP Quiz with Countdown Timer

 

Instant Result of Quiz

As soon as quiz submitted, The user redirect to the result.php  and able to see result of the quiz instantly.

 

  • mysqli_num_rows()  is use to calculate the number question present in the quiz so that we can put the query in for loop.
  • The for loop repeat until all $_POST[$i]  fetch from the previous quiz page index.php 
  • ($row3['ans']==$row3['userans'])  checks if the user answer match with the correct answer if yes, $_SESSION['score'] += 1 ;  score count equal to +1.
  • Each question carry 2 marks so session variable multiplied by two to get actual score.

The preview of the above code is shown in following snapshot:

PHP Quiz with Countdown Timer

 

In this tutorial we have learn to create PHP Quiz with Countdown timer and again in furthermore tutorials we will learn some advance method to improve features of this. So make sure you have connected with us via Subscribe button.

If you have any doubts regarding to this tutorial feel free to ask in the comment section below.

To catch even more knowledgeable and easy understandable tutorials please subscribe to get email notification.

Download Source Code

 

7 thoughts on “PHP Quiz with Countdown Timer

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.