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

matus's avatar

Session flash - output encoding

Hello guys,

I have implemented sweetalert notification system from the Jeff's lesson. However, I am stuck on problem - when I put there characters like this - 'ľščťžýáíé'. I got returned just some code representation of these characters.

This is what my code looks like:

function flash() {
  return session()->flash('flash_message', [
            'title' => 'šššššš',
            'message' => 'message',
            'level' => 'info'
  ]);
}
@if (session()->has('flash_message'))
    <script>
        swal({   
            title: 'session output',
            text:  'session output',
            type:  'session output',
            timer: 1700,
            showConfirmButton: false
        });
    </script>
@endif

If put the characters directly to my view, everything is fine. But when I use sessions, it gets wrong. I suppose it does have something with rendering arrays to json, however I could not find the proper function where would I change something.

How should I solve the problem? Thank you

0 likes
7 replies
phildawson's avatar

As a test

get('one', function(){
    return redirect('two')->with('title', 'ľščťžýáíé');
});

get('two', function(){
    return response()->json(session('title')); 
    // expected "\u013e\u0161\u010d\u0165\u017e\u00fd\u00e1\u00ed\u00e9"
});
1 like
matus's avatar

Yes, it does exactly the same thing as with the alert system!

phildawson's avatar

And another test

get('three', function(){
    return response()->json('ľščťžýáíé');
});

get('four', function(){
    $html = '<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>';
    $html .= '<script>$.ajax({url : "/three", complete: function(r){ console.log(r.responseText); console.log(r.responseJSON) }})</script>';
    return $html;
});

console

"\u013e\u0161\u010d\u0165\u017e\u00fd\u00e1\u00ed\u00e9"
ľščťžýáíé

Seems all fine to me?

matus's avatar

Yes, everything as you've written!

matus's avatar

When I encode it like this - title: json_encode(session('flash_message.title'), it adds me there &quote; Otherwise, everything's fine.

Please or to participate in this conversation.