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

lucassimines's avatar

How to reload session data via Ajax without reloading the page?

Hello guys, I have a Lang switcher menu that change my App`s lang saving it in the session. But, I want to know if it's possible to refresh all view data without reloading the page..

I'm trying the following in my menu:

$(document).ready(function () {

        $('.langs a').on('click', function () {

            var lang_prefix = $(this).data('lang');

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                data: {lang: lang_prefix},
                type: 'GET',
                url: '/lang/'+lang_prefix,
                success: function (result) {
                    sessionData = result.sessionData
                    console.log(sessionData);
                }
            });

        });
    });

And my controller:

public function language(Request $request, $lang)
    {
        $langs = Config::get('app.locales');
        $default_lang = Config::get('app.locale');

        if (array_key_exists($lang, $langs)) {
            $request->session()->put('locale', $lang);
        } else {
            $request->session()->put('locale', $default_lang);
        }
        $request->session()->save();
        return response([
            'sessionData' => session()->all()
        ]);
    }

My Ajax is changing the Language, but the data in my view just change if I refresh the page..

0 likes
11 replies
SaeedPrez's avatar
Level 50

You will most likely need to replace all HTML inside your <body> tag since you're changing language... anyways, you can use jQuery's load() function for that.

lucassimines's avatar

@SaeedPrez Yea.. I'm using now location.reload() in my Ajax success, and it's working very good. How can I use the load() for that? Or it's better using the location.reload()?

lucassimines's avatar

@SaeedPrez got it working! Just a question, when I use the body as target, it keeps loading some meta tags inside it..

SaeedPrez's avatar

@lucassantos you can target an element to get inside the load(), something like this I think, it was long ago I used this..

$( "body" ).load( "article.html body" );
lucassimines's avatar

@SaeedPrez yea it's exactly what I'm doing, tried both ways:

$('body').load(result + ' body')

and

$('body').load(result, ' body')
SaeedPrez's avatar

Try this..

In your blade file, add this wrapper..

<body>
    <div id="reload-wrapper">
        <!-- body content -->
    </div>
</body>

Then do..

$('body').load(result + ' #reload-wrapper')

Edit: This might work as well.. try both see which works better.

<body id="reload-wrapper">
</body>
lucassimines's avatar

@SaeedPrez Thanks! Now it worked, I don't know why it doesn't work with body tag, but no problem using a wrapper tag.

1 like

Please or to participate in this conversation.