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

octabevi's avatar

Refresh session values in view

Hi, i want to refresh session data in view without reloading view.

I have a POST method with ajxa:

$.ajax({
                    type: "POST",
                    url: '/recargarTipoCentro',
                    data: {
                        entidad_id: $('#entidad').val(),
                        region_sanitaria_id: $('#region_sanitaria').val(),
                        territorio_id: $('#territorio').val(),
                        sap_id: $('#sap').val(),
                        up_servicio_id: $('#up_servicio').val()
                    },
                    success: function(data) {

                        if (!data.result) {
                            alert('Hi ha hagut un error');
                            return false;
                        }

                        $.each(data.response.tipo_centro, function(i, e) {
                            $('#tipo_centro').append('<option value="' + e.id + '">' + e.descripcio + '</option>');
                        });

                        @if( session()->has('tipo_centroCalendario') && !empty(session()->get('tipo_centroCalendario')))
                            $('#tipo_centro').val('{{ session()->get('tipo_centroCalendario') }}');
                            $('#tipo_centro').change();
                        @else
                            $('#tipo_centro').val('todos');
                        @endif
                    }
                });
            });

And this is the controller:

    /** RECARGAR CENTROS EN EL SELECTOR */
    public function recargarTipoCentro(Request $request){


        $up_servicios_id    = $request['up_servicio_id'];

        Session::put('up_servicioCalendario', $up_servicios_id);
        Session::forget('tipo_centroCalendario');
        Session::forget('centroCalendario');
        Session::forget('ubicacionCalendario');

        $up_servicios   = DB::table('gen_a_up_servei')
                        ->where('UP_ID_UP', $up_servicios_id)
                        ->distinct()->pluck('UP_CODI_LDAP')->toArray();
        
        $centros        = DB::table('gen_a_centres')
                        ->select('tipus')
                        ->where('CODIGO_EXT', $up_servicios)
                        ->whereNull('DATA_BAIXA')
                        ->distinct()->pluck('tipus')->toArray();

        $tipo_centro    = DB::table('gen_a_centres_tipus')
                        ->orderBy('descripcio')
                        ->select('id', 'descripcio')
                        ->whereIn('id', $centros)
                        ->get()->toArray();

        return response()->json(['result' => true, 'response' => array('tipo_centro' => $tipo_centro)]);
    }
0 likes
2 replies
LaryAI's avatar
Level 58

To refresh session data in the view without reloading the page, you can use JavaScript to make an AJAX request to the server and update the session data in the view based on the response. Here's an example:

// Make an AJAX request to the server
$.ajax({
    type: "POST",
    url: "/refresh-session-data",
    success: function(response) {
        // Update the session data in the view
        $("#session-data").text(response.sessionData);
    }
});

In this example, we're making a POST request to the "/refresh-session-data" URL and updating the session data in the view based on the response. You'll need to replace "/refresh-session-data" with the URL of your server-side script that updates the session data.

On the server side, you can update the session data and return a JSON response like this:

// Update the session data
$_SESSION["data"] = "new data";

// Return a JSON response
header("Content-Type: application/json");
echo json_encode(["sessionData" => $_SESSION["data"]]);

In this example, we're updating the "data" key in the session and returning a JSON response with the updated session data. You'll need to replace "data" with the key that you want to update in your session.

octabevi's avatar

Can you replay that example using Laravel ?

Please or to participate in this conversation.