1

The script never hits the route to set the session variable.

Script that sends the data that needs to be put into a session variable.

<script>
         $(document).ready(function() {             
                var timezone = "PST";

                $.ajax({
                    type: "POST",
                    url: "/set_session",
                    data: { "timezone": timezone },
                    success: function(){
                        location.reload();
                    }
                });
            })
    </script>

- Here is my route that will be used to set the session variable.

Route::post('/set_session', 'SetSessionsController@create');

- This is my controller that sets the session.

class SetSessionsController extends Controller
{

    public function create(Request $request)
    {
        session(['timezone' => $request->get('timezone');
    }

}
3
  • are there errors in the console? Commented Mar 8, 2018 at 1:07
  • Make it return your session Commented Mar 8, 2018 at 1:09
  • It something wrong with this line session(['timezone' => $request->get('timezone'); when I comment it out the ajax is successful Commented Mar 8, 2018 at 1:18

1 Answer 1

2

You have a typo there:

session(['key' => 'value']);

So your code should be:

session(['timezone' => $request['timezone']]);

You can Access it in the blade template using:

@if(Session::has('timezone')))  
  {{ Session::get('timezone')}}
@endif
Sign up to request clarification or add additional context in comments.

1 Comment

Ok now the request is successful. How would I use the session variable it in my html?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.