How to save two variables from a JS in php session?


I wonder if there is a way to save these two variables. Here is the code:

<script type="text/javascript">
 function opcao()
                var empresa = $('#Empresa option:selected').val();
                $('#Unidades').load('/site/funcoes2.php?emp='+ empresa);
 <script type="text/javascript">
     function uniopcao()
            $(document).ready(function () 
                var unid = $('#Unidades option:selected').val();
                $('#unn').load('/site/tpago.php?uni='+ unid);  

I would like to take these two variables (variable "companies" that receives through an onchange the selected company) and a (variable "unid" that receives the chosen unit according to the company chosen. I wanted to assign these values to a SESSION PHP as in the example below.

_SESSION['V1']=  empresa;
_SESSION['V2'] = unid;

I'll use these two information throughout the rest of the program so I'd like to save them. Thank you.

asked by anonymous 18.05.2016 / 14:30

2 answers


In your "funcoes2.php" file add the line:

$_SESSION['V1'] =  $_GET['emp'];

In your "tpago.php" file add the line:

$_SESSION['V1'] =  $_GET['uni'];

In this way, whenever your ".load ()" loads the url, the session will set the variables you want.

18.05.2016 / 14:44

You will need to use JavaScript to send the variable to a new link: pagina.php?empresa=1&uni=1

Then in this new page you use $_GET :

$_SESSION['V1']= $_GET["empresa"];
$_SESSION['V2']= $_GET["uni"];
18.05.2016 / 14:49