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

johnnw's avatar

How to pass data from one view to another using session?

I have a conference details page and a page to register in the conference.

In the conference details page, the user can select through a select menu how many registrations want from each registration type available for the conference and then click in the "Next" button to go to the registration page.

My doubt is how to pass data (quantity of each registration type selected) from conference details page to the registration page? Because in the registration page is necessary to show a summary of the quantity of registration selected by the user in the conference details page, a summary like:

registration_types       quantity             price          subtotal
rt01                                       2                      5.00$           10.00$    
rt02                                       1                      10.00$          10.00$                 
TOTAL                                                                               20.00$

For what Im understanding is necessary to use sessions for this context, but I dont find any example of using sessions to pass data from another page like this context. Do you know how this can be achieved?

The structure I have is:

FrontEndController that has a show method to show the conference details page:

public function show($id, $slug){
        $conf = Conference::where('id', $id)->first();
        $registrationTypes = RegistrationType::where('conf_id', $conf->id)->get();
        return view('confs.show')->with('conf',$conf)->with('registration_types', $registrationTypes);
    }

The route for this method above:

Route::get('/conference/{id}/{slug?}', [
    'uses' => 'FrontEndController@show',
    'as'   =>'confeerences.show'
]);

In the conference details page I have select menus so the user can select how many tickets want from each registration type:

<div>
    <div>
        <span>Registration</span>
        <span>Quantity</span>
        <span>Price</span>
    </div>
    <div>
        <ul>
            @foreach($registration_types as $rtype)
                <li>
                    <div>
                        <span>{{$rtype->name}}</span>
                    </div>
                    <form>
                        <select
                                 id="rtype_{{ $rtype->id }}" data-price="{{ $rtype->price }}" name={{ $rtype->name }}>
                            <option selected></option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                               <option>4</option>
                        </select>
                </form>
                <span>X {{$rtype->price}}€</span>
                </li>
            @endforeach
            <li>
                <div>
                    <span>TOTAL</span>
                </div>
                <span>0.00€</span>
            </li>
        </ul>
    </div>
    <div>
        <a  href="{{route('conferences.registration', ['id' => $conf->id, 'slug' => $conf->slug])}}">Next</a>
    </div>
</div>

After the user selects the number of registrations for each registration type that he wants he can click "Next" to go to the registration page.

So I created this route:


Route::get('/conference/{id}/{slug?}/registration', [
    'uses' => 'FrontEndController@registration',
    'as'   =>'conferences.registration'
]);

And in FrontEndController I have the method:

public function registration(){
        return view('conferences.registration');
    }

Registrtion page summary, that is static for now in the registration.blade.php file:

 <div>
    <div>
        <span>Summary</span>
    </div>
    <div>
        <ul>
            <li>
                <span>Registration Type</span>
                <span>Quantity</span>
                <span>Price</span>
                <span>Subtotal</span>
            </li>
            <li>
                <span>rt01</span>
                <span>2</span>
                <span>5.00$</span>
                <span>10.00$</span>
            </li>
            <li>
                <span>rt02</span>
                <span>1</span>
                <span>10.00$</span>
                <span>10.00$</span>
            </li>
            <li>
                <span>TOTAL</span>
                <span>20.00€</span>
            </li>
        </ul>
    </div>
</div>

0 likes
6 replies
Snapey's avatar
Snapey
Best Answer
Level 122

no need for sessions. Your next button submits the first form to the controller with a Post route, you can then pass whatever you need from that form into the next view.

1 like
johnnw's avatar

Thanks, you are saying to store the quantity of each registration type selected by the user in the conference details page into the database when "Next" button is clicked?

But for example if the user selects that we want 2 tickets of the registration types "rt1" and 1 of the registration type "rt2" maybe don't make sense to store that information on database even because the user may end up not finishing the registration after click in "Next" to go to the registration page.

Snapey's avatar

no, im not saying you have to persist it.

Im saying you can pass it to your next form

1 like
johnnw's avatar

Thanks, but I don't have a next form. When the user clicks "Next" button he goes to another page, to the registration page, where appears the summary above and also some fields for the user to fill in. Its possible to pass the info from the conference details page to the registration page to show the summary with the correct dynamic values after the user click in "Next" without store that info anywhere? I'm not understanding how that works.

Snapey's avatar

but I don't have a next form.

where appears the summary above and also some fields for the user to fill in.

The data passed from the previous form can be used to display the summary AND populate hidden fields in the registration form so that your second Post has all data passed to it

jlrdw's avatar

@Snapey has good answer, but if you have put data into session, it should be available in another view / controller anyway.

Edit, some old code I no longer use

<?php
$areturn = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$haystack = $areturn;
$needle = '?';
$pos = strripos($haystack, $needle);
if ($pos === false) {
    $areturn = $areturn . "?page=1";
    Illuminate\Support\Facades\Session::put('areturn', $areturn);
} else {
    Illuminate\Support\Facades\Session::put('areturn', $areturn);
}
echo Illuminate\Support\Facades\Session::get('areturn');
?>

But here I store the page I am on, so after an edit I can return to same index page.

Are you having trouble with a session persisting?

After the edit, I simply use this in controller

$vurl = Session::get('areturn');
            return redirect($vurl);
1 like

Please or to participate in this conversation.