9

I have a javascript code like this :

<script type="text/javascript">

    $('#editRole').on('show.bs.modal', function (e) {  

        $roleID =  $(e.relatedTarget).attr('data-id');
        // Here I want to set this $roleID in session may be like this :
        Session['roleID'] = $roleID;                      
    });    

</script>

Then I want to get that $roleID in an other place using php code, may be like this :

<?php $roleID = Session::get('roleID'); //do something ....  ?>

Thanks

1
  • 2
    You shouldn't, and you can't, set PHP session variables from the clientside. You'd have to send something to the server so the server sets it, but you need to be careful so you don't expose the entire superglobal. Commented Dec 30, 2015 at 20:40

2 Answers 2

16

You can't set a server session variable directly from JS.

To do that you can make an AJAX call to a PHP script passing the value you want to set, and set it server side:

$('#editRole').on('show.bs.modal', function (e) {  

    $roleID =  $(e.relatedTarget).attr('data-id');

    //ajax call 
    $.ajax({
         url: "set_session.php",
         data: { role: $roleID }
    });                             
}); 

set_session.php

//preliminary code

Session::put('roleID', $request->input('role') );                      
Sign up to request clarification or add additional context in comments.

2 Comments

just to set session variable need ajax call?
is it possible to set session directly in javascript code with laravel?
3

Above answer and from other resources together helped me out to make example similar to situation like 'Setting the session using AJAX in laravel'.

Im posting simple example, which other users might found this helpful.

View - ajax_session.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#submit').on('click',function(){
                // show that something is loading
                $('#response').html("<b>Loading response...</b>");
                $.ajax({
                    type: 'POST',
                    url: '/set_session',
                    data: $("#userform").serialize()
                })
                .done(function(data){
                    // show the response
                     $('#response').html(data);
                })
                .fail(function() {
                    // just in case posting your form failed
                    alert( "Posting failed." );
                });
                // to prevent refreshing the whole page page
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="userform">
        {{ csrf_field() }} <!--required - otherwise post will fail-->
        <input type="text" id="uname" name="uname" required/>
        <button type='submit' id="submit">Submit</button>
        <div id='response'></div>
    </form>
</body>
</html>

routes.php

Route::get('session_form', function () {
    return view('ajax_session');
});
Route::post('set_session', 'SessionController@createsession');
Route::get('allsession', 'SessionController@getsession');

Controller - sessionController.php

public function createsession(Request $request)
{
    \Session::put('uname', $request->uname);
    echo "session created";
}
public function getsession()
{
    dd(\Session::get('uname'));
}

You can check this by running localhost:8000/session_form. And you can also check session separately by localhost:8000/allsession.

Hope this helps!!!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.