1

Set Session Variable Using JavaScript in PHP

In above code snippet I have initialized global variable myvar with value stored into php session variable into script file before general.js file is referenced in script.It will be accessible in general.js file

How to access Session variables and set them in javascript?

Accessing & Assigning the Session Variable using Javascript:

Assigning the ASP.NET Session Variable using Javascript:

 
function SetUserName()
var userName = "Shekhar Shete";
'';
alert('');
>

Accessing ASP.NET Session variable using Javascript:

 
function GetUserName()

var username = '';
alert(username );
>

Clearing a Session variable using javascript

Here is what I have figured out to work

$(function () $('.close').click('.close', function() $.get('php/scripts/clear/employeeError.php', function(data) alert("Server Returned: " + data); //Used to verify script runs 
>);
return false;
>);
>);

Basically what happens now is I use this inline script to listen for the .close and then it will call the script to unset the session variable.

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

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

Читайте также:  Php fopen to string

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

How do I make a PHP session variable equals to JS variable?

You can use Ajax for that:

var httpRequest = null;
function getHttpObject()
if (httpRequest != null)
return;

if (window.XMLHttpRequest) < // Mozilla, Safari, .
httpRequest = new XMLHttpRequest();
> else if (window.ActiveXObject) < // IE 8 and older
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
>
>

function getLocation()
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(showPosition);
>
else
>

function showPosition(position)
<
document.getElementById('inputfield3').value = position.coords.latitude;
document.getElementById('inputfield4').value = position.coords.longitude;

getHttpObject();

// I don't know how to get the positions for showPosition, so you may have to change this part.
httpRequest.open("GET", "set_position.php?lat=" + position.coords.latitude + "&lon post" action="set_positions.php" >

function getLocation()
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(showPosition);
>
else
>

function showPosition(position)
<
document.getElementById('inputfield3').value = position.coords.latitude;
document.getElementById('inputfield4').value = position.coords.longitude;
document.getElementById('form1').submit();
>

So you’ll receive those fields in your PHP script. Note that those fields can be hidden fields too if you doesn’t want to user to change its value.

How to insert javascript value into the php session variable

You cannot access it directly (the way you are doing). However, it can be done using AJAX.
Here is the perfectly working solution.