How can I set a session var using javascript and get it via php code

0 votes

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 in advance!

Jun 11, 2020 in Java-Script by kartik
• 37,530 points
10,240 views

2 answers to this question.

0 votes

Hello @kartik,

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') );    

Hope this is helpful!

To  know more about javascript, join comprehensive Java training course online.

answered Jun 11, 2020 by Niroj
• 82,820 points
0 votes
Yes it is possible. You can either use document.cookie, window.localStorage or window.sessionStorage.
But window.localStorage and sessionStorage are only supported in modern Browsers.
So, you should check for browser's compatibility before proceeding.
[CODE]
<script>
function saveVariable()
{
if(typeof(Storage) == "undefined")
{
alert ("local storage not supported by this Broswer");
}
else
{
localStorage ['test'] = 'Saving to local storage'];
}
}

function read()
{
alert (localStorage ['test']);
}
</script>

[/CODE]

You can check https://www.emmason247.com.ng/tutorial/javascript-persistent-variable-storage-using-localstorage-and-sessionstorage/GGZIGWWZD
 for more information on sessionStorage and localStorage
answered Aug 9, 2020 by Okugbe
• 280 points

Related Questions In Java-Script

0 votes
1 answer

How can I allow django admin to set a field to NULL?

Hello @kartik, Try to overwrite the save() method ...READ MORE

answered Jun 12, 2020 in Java-Script by Niroj
• 82,820 points
1,733 views
0 votes
1 answer

How do I pass variables and data from PHP to JavaScript?

Hello @kartik, Simply use one of the following ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,820 points
558 views
0 votes
1 answer

How can I call PHP functions by JavaScript?

Hello @kartik, You can do Ajax request to ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,820 points
9,255 views
0 votes
1 answer

How do I run PHP code when a user clicks on a link?

Hello @kartik, You'd need to have a javascript ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,820 points
2,516 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
9,508 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
1,054 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
854 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
10,608 views
0 votes
2 answers

How can I set focus on an element in an HTML form using JavaScript?

Hi Kartik, try the following script <script>  (window.onload = ...READ MORE

answered Sep 24, 2020 in Java-Script by Okugbe
• 280 points
163 views
0 votes
1 answer

How can I submit a form using JavaScript?

Hello @kartik, Set the name attribute of your form to "theForm" and ...READ MORE

answered Oct 9, 2020 in Java-Script by Niroj
• 82,820 points
92 views