Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lara_Love's avatar

Building an online test site

Hello To build an online test site, how to create time to display each question? How to create every page that has a question in it? (time can be changed by admin)

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To create an online test site with timed questions, you can use Laravel and JavaScript. Here's a basic outline of how you can approach this:

  1. Create a database schema for your questions and answers. You'll need tables for questions, answers, and users (if you want to track who is taking the test).

  2. Create a Laravel controller to handle the logic for displaying questions and answers. You'll need methods for displaying the test, submitting answers, and calculating scores.

  3. Use JavaScript to create a timer that counts down the time for each question. You can use the setInterval() function to update the timer every second.

  4. Use Laravel's pagination feature to display one question per page. You can set the number of questions per page in the admin panel.

  5. In the admin panel, create a form to set the time limit for each question. You can store this value in the database and use it to set the timer for each question.

Here's some sample code to get you started:

// Controller method to display a single question
public function showQuestion($id) {
    $question = Question::find($id);
    return view('question', ['question' => $question]);
}

// JavaScript timer function
function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.text(minutes + ":" + seconds);

        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

// Blade template for displaying a question
<div class="question">
    <h2>{{ $question->title }}</h2>
    <p>{{ $question->description }}</p>
    <form action="/submit-answer" method="POST">
        @csrf
        <input type="hidden" name="question_id" value="{{ $question->id }}">
        <input type="radio" name="answer" value="A"> {{ $question->answer_a }}<br>
        <input type="radio" name="answer" value="B"> {{ $question->answer_b }}<br>
        <input type="radio" name="answer" value="C"> {{ $question->answer_c }}<br>
        <input type="radio" name="answer" value="D"> {{ $question->answer_d }}<br>
        <button type="submit">Submit</button>
    </form>
    <div class="timer"></div>
</div>

// JavaScript code to start the timer
var timeLimit = {{ $question->time_limit }};
var display = $('.timer');
startTimer(timeLimit, display);

This is just a basic example, but it should give you an idea of how to approach building an online test site with timed questions. Good luck!

Please or to participate in this conversation.