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

red_devil's avatar

Save radio button selection value in session variable

Hello All,

I'm building a multiple page quiz.

When users select options(answer) for a question, it should store it in a session variable. If he needs to change the answer later, he can(and update the session variable accordingly). I have 20 questions and an equal number of pages in total.

How to achieve this to store session value? Or is there any other approach suggested.

PS: I am a noob when it comes to building complex functions. A point in the right direction would be appreciated.

This my code:

question1.blade.php

<body>
<div class="container">
<div class="privew">
    <div class="questionsBox">
        <div class="questions"><h1>Something</h1></div>
        <div class="answerList"><h3>You are only allowed to select one option from the given below question</h3></div>
        <div class="questions"><h4>Question 1</h4></div>
        <div class="questions"><h3>Section 1</h3></div>
        <ul class="answerList">
            <li>
                <label>
                    <input type="radio" name="answerGroup" value="0" id="answerGroup_0">Yes</label>
            </li>
            <li>
                <label>
                    <input type="radio" name="answerGroup" value="1" id="answerGroup_1">Rather yes</label>
            </li>
            <li>
        </ul>
    </div>
        <div class="questionsBox">
            <div class="questions"><h3>Section 2</h3></div>
            <ul class="answerList">
                <li>
                    <label>
                        <input type="radio" name="answerGroup" value="2" id="answerGroup_2">Yes</label>
                </li>
                <li>
                    <label>
                        <input type="radio" name="answerGroup" value="3" id="answerGroup_3">Rather yes</label>
                </li>
            </ul>
        </div>
        <div class="questionsRow"><a href="{{ route('ques2') }}" class="button" id="nextquestions">Next</a><span>1 from 20</span><a href="{{ route('ques20') }}" class="button" id="nextquestions">Previous</a></div>
    </div>
</div>
<script type="text/javascript">

</script>
</body>

TestsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestsController extends Controller
{

    public function store(Request $request){

        $answers = $request->get('answer[]',0);
        dd('answers');
        return view('ques1');


        }
}
0 likes
7 replies
rouge's avatar

Is there any specific reason you want to store them in sessions? Why not store them straight to database?

bobbybouwmann's avatar

Laravel has this helper method for sessions and a facade for sessions. However that is also in the documentation available: https://laravel.com/docs/6.x/session#using-the-session

In your case you might have something like this

// Facade
Session::put('answers.' . $question_id, $request->get('answer')); 

// Helper
session(['answers.' . $question_id => $request->get('answer')]); 

// Request
$request->session()->put('answers.' . $question_id, $request->get('answer'));

Sessions are key value based, so you can store in them whatever you want. Either an array with all the answers or a single key value based approach like I showed above.

Let me know if this makes thing clearer or not at all!

red_devil's avatar

Hi, @rouge847 thanks for your response. I am open to store all my selections at the end with just a single submit button. The problem is that I have 20 questions and all are located on different pages so I need to store the selection of every page and in case the user moves back and forth as well. However, I found the sessions approach over the internet but I am not very confident about it. Do you have any suggestion to implement this in some another way? Suggestions are welcomed.

red_devil's avatar

Thank you sir @bobbybouwmann for replying. I will definitely look into the docs. But as I said this was a suggested approach but I am open to another in case I need a different. Thanks again!

bobbybouwmann's avatar

@red_devil Saving in the session or the database is basically the same approach. You can still edit the answer even if you stored it before in the database. The extra benefit of storing it in the database is that the user can continue the quiz at a later stage. If this is not part of your use case than the sessions is fine ;)

Goodluck :D

Please or to participate in this conversation.